1. Background
In Android, threads are divided into the main thread and sub-threads, and the main thread deals primarily with interface-related things, while child threads are often used to perform time-consuming operations.
In addition to the thread itself, you can play thread roles in Android as well as Asyntask and Intentservice, while Handlerthread is also a special thread.
For Asyntask, it uses the thread pool at the bottom, asyntask encapsulates the thread pool and handler, primarily to make it easier for developers to update the UI in child threads.
For Intentservice and Handlerthread, they use threads directly at the bottom. Handlerthread is a thread with a message loop that can use handler inside it.
Intentservice is a service that the system encapsulates to make it easier to perform background tasks, Intentservice internally with Handlerthread to perform tasks, and Intentservice automatically exits when the task is completed.
The role of the Android main thread is to run four components and handle their interactions with the user, while the child threads perform time-consuming tasks such as network requests, IO operations, and so on.
2, Asynctask
Asynctask is a lightweight asynchronous task class that can perform background tasks in a thread pool and then pass the progress and final results of execution to the main thread and update the UI in the main thread. Asynctask is not suitable for particularly time-consuming background tasks, it is recommended to use a thread pool for particularly time-consuming tasks.
Asynctask is an abstract generic class:
Public abstract class Asynctask<params,progress,result>
Four core approaches are available:
A, OnPreExecute (), executed in the main thread, this method is called before the asynchronous task executes, and can generally be used to do some preparatory work.
B, Doinbackground (params ... params), executed in the thread pool, which is used to perform asynchronous tasks. The Publishprogress method can be used to update the progress of a task, and publishprogress calls the Onprogressupdate method. In addition, this method needs to return the result to the OnPostExecute method.
C, Onprogressupdate (Progress. Values), executed in the main thread, which is called when the execution progress of the background task changes.
D, OnPostExecute (result result), executed in the main thread, is called after the asynchronous task executes, where the result parameter is the return value of the background task, which is the return value of Doinbackground.
In addition to the methods described above, Asynctask also provides the oncancelled () method, which is also executed in the main thread, and when the asynchronous task is canceled, the oncancelled () method is called, and OnPostExecute is not invoked at this time.
Attention:
A, Asynctask class must be loaded in the main thread;
b, the Asynctask object must be created in the main thread;
C, the Execute method must be called on the UI thread;
D, a Asynctask object can only be executed once, that is, the Execute method can be called only once, otherwise it will report the running exception.
3, Handlerthread
Handlerthread inherits thread, which is a thread that can use handler, and its implementation is to create a message queue through Looper.prepare () in the Run method, and to turn on the message loop through Looper.loop (). This allows the creation of handler in Handlerthread in the actual use.
4, Intentservice
Intentservice is a special service that inherits the service and is an abstract class, so you must create its subclasses to use Intentservice.
Android Threads and thread pool