thread pool:
A thread pool caches a certain number of threads, which can be avoided by the thread pool because of the overhead of frequently creating and destroying threads. The thread pool in Android originates from Java, and is primarily derived from executor to derive a specific type of thread pooling.
Advantages: nbsp , &NB Sp , &NB Sp , &NB Sp , &NB Sp , &NB Sp 1. Reuse threads in the thread pool to avoid the performance overhead associated with creating and destroying threads. 2, avoid a large number of threads due to each other to seize the system resources caused by blocking phenomenon; 3, the ability to simple management of the thread and provide timed execution, interval execution and other functions;
The thread pool in Android comes from Executor,executor is an interface, the real thread pooling is implemented as Threadpoolexecutor, and the thread pool is configured through a series of parameters provided by Threadpoolexecutor. Different parameters can create different thread pools. Among the four types of thread pools common to Android are: Fixthreadpool, Cachedthreadpool, Scheduledthreadpooi, Singthreadexecutor. In addition to the above four thread pools, we can set parameters to customize the thread pool as appropriate.
First, let's look at Threadpoolexecutor in Android, which constructs a set of parameters to configure the thread pool. The following is a more common construction method.
corepoolsize: The number of core threads, the core thread will remain alive, even if no tasks need to be handled. If you set the Allowcorethreadtimeout property of Threadpoolexecutor to True, the idle core thread waits for a new task to arrive with a time-out policy that is specified by KeepAliveTime. When the wait time exceeds the length specified by KeepAliveTime, the core thread is terminated.
maximumpoolsize: The maximum number of threads that the thread pool can hold, and when the number of active threads reaches this value, subsequent new tasks will be blocked.
KeepAliveTime: When the thread idle time reaches KeepAliveTime, the thread exits until the number of threads equals Corepoolsize. If Allowcorethreadtimeout is set to True, all threads exit until the number of threads is 0.
Unit: The time unit used to specify the KeepAliveTime parameter, which is an enumeration, commonly used with
Timeunit.milliseconds, Timeunit.second, Timeunit.minutes.
workQueue: The task queue in the thread pool, and the Runnable object that is submitted by the Execute method of the wire pools is stored in this parameter.
threadfactory: Thread Factory, which provides the thread pool with the ability to create new threads.
Threadpoolexecutor follows the following rules when performing a task: , &NB Sp , &NB Sp , &NB Sp 1. If the number of thread pools does not reach the number of core threads, a core thread is started directly to execute Task. , &NB Sp 2. If the number of threads in the thread pool has reached or exceeds the number of core threads, then the task is inserted into the task queue and queued for execution. 3. If a task cannot be inserted into the task queue in step 2, often because the task queue is full, this time if the number of threads does not reach the maximum specified by the thread pool, a non-core thread is started to execute immediately. 4. If the number of threads in step 3 has reached the maximum specified by the thread pool, then the task is denied. Threadpoolexecutor calls Rejectedexecutionhandler's Rejectedexecution method to notify the caller.
Four types of common thread pools:
1, Fixedthreadpool: Through the executors Newfixedthreadpool method to create. It is a thread pool with a fixed number of threads that are not recycled when the threads are idle, unless the thread pool is closed. There are only core threads in Fixedthreadpool and these core threads do not have a timeout mechanism, and there is no size limit for the task queue.
Called in the program:
2, Cachedthreadpool: It is a thread pool of variable number of threads, the maximum number of threads can be arbitrarily large. This type of thread pool is better suited to perform a large number of less time-consuming tasks. When the entire thread pool is idle, threads in the thread pool will time out and be stopped, and this time the Cachedthreadpool actually has no threads, and it consumes almost no system resources.
Called in the program:
3.ScheduledThreadPool: Its number of core threads is fixed, not the number of core threads is unlimited, and is immediately recycled when non-core threads are idle. This type of thread pool is primarily used to perform scheduled tasks and repetitive tasks with fixed cycles.
Called in the program:
4. Singlethreadexecutor: There is only one core thread inside its thread pool, which ensures that all tasks are executed sequentially in the same thread. The point is to unify all the outside tasks into one thread, so that there is no need to handle thread synchronization between these tasks.
Called in the program:
Custom thread pool:
The comments in the code have explained in detail how the thread pool is implemented, which is no longer a proud statement.
Android thread pool-Learning Summary