Android Development (6) asynctask

Source: Internet
Author: User

1: Is asynctask multithreading?
A: Yes.
2: Who is more lightweight than handler in asynctask?
A: By looking at the source code, we found that asynctask is actually a thread pool. The online saying is that asynctask is lighter than handler and is obviously inaccurate.
AsynctaskCodeIt is less lightweight than handler, but actually it consumes more resources than handler, because asynctask is a thread pool at the underlying layer! While
Handler only sends a message queue, and the connection process is not enabled.
However, if the data of an asynchronous task is extremely large,AsynctaskThe advantages of this thread pool structure are shown.

The advantages of asynctask are as follows:
• The thread overhead is high. If a thread is created for each task, the efficiency of the application process is much lower;
• Threads cannot be managed. Anonymous threads are not allowed to be created and started.ProgramIf many requests are sent, many threads will be started and the system will be overwhelmed.
• As we can see earlier, handler must be introduced to update the UI in the new thread, which makes the Code look very bloated.

Asynctask defines three generic types: Params, SS, and result.
• Input parameters for Params startup task execution, such as the URL of the HTTP request.
• Percentage of progress SS background tasks executed.
• Result: The result returned when a task is executed in the background, such as string.

Asynctask is executed in four steps. Each step corresponds to a callback method. developers need to implement one or more methods. These methods are automatically called During task execution.

Onpreexecute (), this method will be called by the UI thread before the actual background operation is executed. You can make some preparations in this method, such as displaying a progress bar on the interface.

Doinbackground (Params ...),
The method is executed immediately after it is executed. The method runs in the background thread. Here, we will primarily perform those time-consuming background computing tasks. Yes
Publishprogress method to update the real-time task progress. This method is an abstract method and must be implemented by sub-classes.

Onprogressupdate (Progress...), after the publishprogress method is called, UI thread will call this method to display the progress of the task on the interface, for example, display through a progress bar.

Onpostexecute (result). After doinbackground is executed, the onpostexecute method will be called by the UI thread, and the background computing result will be passed to the UI thread through this method.

when using the asynctask class, the following rules must be followed:
1) The task instance must be created in the UI thread
2) execute method must be called in Ui thread
3) do not manually call onpreexecute (), onpostexecute (result), doinbackground (Params ...), onprogressupdate (Progress ...)
4) The task can only be executed once. Otherwise, an exception occurs during multiple calls.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.