Brief introduction of thread pool ThreadPoolExecutor of Android

  • 2021-11-02 02:18:48
  • OfStack

Thread pool ThreadPoolExecutor in Android solves the problems of slow efficiency and thread blocking of single thread downloading data, and its application is also an optimized way to realize it. Therefore, its importance is self-evident, but its complexity is also great, and there may be problems in understanding it. However, as an Android engineer, it is inevitable to understand this.

ThreadPoolExecutor has several constructors, and the constructor with the most parameters is the most commonly used. The meaning of each parameter and the relationship between several parameters will be described in detail below:


<span style="font-size:18px;">ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler)</span>

Meaning of each parameter:

corePoolSize Number of core threads, number of tasks that can be executed simultaneously

maximumPoolSize Excluding tasks waiting in the buffer queue, the maximum number of tasks that can be accommodated (actually including the number of core thread pools)

keepAliveTime Time to wait for tasks beyond workQueue Time to live 1 thread placed by maximumPoolSize

unit Time unit

workQueue Blocking the queue of waiting threads, 1 using new LinkedBlockingQueue<Runnable>() This, if you do not specify the capacity, will be 1 straight into the inside to add, there is no limit, workQueue will never be full;

threadFactory Create a factory for threads, using the system default classes

handler When the number of tasks exceeds maximumPoolSize, the default policy for processing tasks is to refuse to add

It should be noted that handler here is different from handler in Android

Execution process:

When the number of threads is less than corePoolSize, the thread will be started immediately for every task added

When corePoolSize is full, the tasks added later will be put into the buffer queue workQueue to wait;

When workQueue is also full, see if it exceeds the number of maximumPoolSize threads. If it exceeds, it refuses to execute by default

To sum up: core thread corePoolSize, task queue workQueue, and maximum thread maximumPoolSize. If all three are full, use handler to handle rejected tasks. When the survival time of a thread reaches the specified survival time, the task of the thread has been executed, and the life cycle of the thread ends. The rejected thread can re-enter the thread pool to start the task, and cycle this process in turn.

For example:

Suppose:

corePoolSize=2, maximumPoolSize=3, workQueue capacity is 8;

At the beginning, the tasks A and B are executed. At this time, corePoolSize has been used up. If C is executed again, C will be put into the buffer queue workQueue and wait. If 7 tasks are added later and workQueue is full at this time, the number of tasks coming later is 5, which will be compared with maximumPoolSize. Because maximumPoolSize is 3, it can only accommodate 1.

Summarize


Related articles: