On the use of Android thread and thread pool

Android threads and thread pools

In terms of purpose, threads are divided into main threads and sub threads; The main thread mainly deals with things related to the interface, while the sub thread is often used for time-consuming operations.

Main thread and sub thread

The main thread refers to the thread owned by the process. In Android, the main thread intersects with the UI thread, which is mainly used to run the four components and handle their interaction with users; The job of a child thread is a time-consuming task.

Thread form in Android

1. Asynctask asynctask is a lightweight asynchronous task class. It can execute background tasks in the thread pool, then pass the execution progress and final results to the main thread and update the UI in the main thread. Asynctask is an abstract generic class that provides params (parameter type), progress (background task execution progress type) and result (the type of the returned result of the background task) these three generic parameters, asynctask provides four core methods

Onpreexcute is executed first, followed by doinbackground, and finally onpostexecute. When the asynchronous task is cancelled, the oncancelled () method will be called, and onpostexecute will not be called at this time.

2. Some restrictions on the use of asynctask

3. Working principle of asynctask asynctask has two thread pools (serialexecutor and thread_pool_executor) and one handler (internalhandler). The thread pool serialexecutor is used for task queuing, and the thread pool thread_ POOL_ Executor is used to actually execute tasks, and internalhandler is used to switch the execution environment from the thread pool to the main thread.

4. Handlerthread handlerthread inherits thread and is a thread that can use handler. Its implementation is to create a message queue through loop. Prepare() in the run method and open a message loop through loop. Loop().

Compared with ordinary threads, ordinary threads are mainly used to execute a time-consuming task in the run method, while handlerthread creates a message queue internally, and the outside world needs to notify handlerthread to execute a specific task through the handler's message.

Because the run method of handlerthread is an infinite loop, when handlerthread is explicitly not needed, the thread execution can be terminated through its quit or quitsafe methods.

5. Intentservice intentservice is a special service that inherits service and is an abstract class. Its subclass must be created to use intentservice. Intentservice can be used to execute background time-consuming tasks. After the task is executed, it will automatically stop, and its priority is much higher than that of a simple thread. It is not easy to be killed by the system. In implementation, intentservice encapsulates handlerthread and handler.

Thread pool in Android

Advantages of thread pool

ThreadPoolExecutor ThreadPoolExecutor is the real implementation of a thread.

The rules that ThreadPoolExecutor follows when executing tasks

Classification of thread pools

Four typical usage methods of thread pools are preset:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>