Thread pool to learn and corresponding sdk in comments translated

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/One_Month/article/details/81509674

Android sometimes used in many threads to deal with the problem, but it is unwise to open multiple threads, there will be a big overhead, this time you can use the thread pool.

A. The benefits of the thread pool
1. taking a thread, to avoid repeatedly create and shut down threads to produce overhead
2. controls the maximum number of concurrent threads

Create a second .Android in the thread pool
in Android can use the Executors factory methods to create a thread pool, but they are actually calling the
new ThreadPoolExecutor () method to create, but in a different constructor parameters passed, the
order may look at ThreadPoolExecutor class constructor parameter and its meaning.

/**
 * Creates a new {@code ThreadPoolExecutor} with the given initial
 * parameters and default thread factory and rejected execution handler.
 * It may be more convenient to use one of the {@link Executors} factory
 * methods instead of this general purpose constructor.
 *
 * @param corePoolSize    核心线程数,保持在线程池中的线程数,即使它
 *                        们是空闲的,除非allowCoreThreadTimeOut
 *                        被设置,否则他们是不会被自动回收的
 * @param maximumPoolSize 允许在线程池中的最大线程数量,包含核心线程
 *                        数和非核心线程数,如果设置超时时间,非核心
 *                        线程数空闲超时会被回收掉
 * @param keepAliveTime   超时时间,如果线程数超过了核心线程数(也就是
 *                        有非核心线程),非核心线程如果在这个时间内没
 *                        有新的任务就会被回收allowCoreThreadTimeOut
 *                        被设置为true的话,这个时间也会作用到核心线程
 *                        ,也就是说核心线程也会超时被回收
 * @param unit            keepAliveTime的时间单位(秒,微秒。。。)
 * @param workQueue       保存还未执行的任务的队列
 * @throws IllegalArgumentException if one of the following holds:<br>
 *         {@code corePoolSize < 0}<br>
 *         {@code keepAliveTime < 0}<br>
 *         {@code maximumPoolSize <= 0}<br>
 *         {@code maximumPoolSize < corePoolSize}
 * @throws NullPointerException if {@code workQueue} is null
 */
public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue) {
    this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
         Executors.defaultThreadFactory(), defaultHandler);
}

"`
ThreadPoolExecutor perform tasks generally follow the following rules
1. The number of threads running the core does not exceed the number of threads, the thread will directly start the core mission
2. Core threads are on a mission, follow-up mission will be inserted into the queue waiting (so CachedThreadPool
maximum number of threads basic infinity, but can not save the task queue, the task will be executed immediately)
3. under condition 2, if the queue was filled with the task, if the number does not exceed the maximum number of threads provisions, it will start
a thread to handle non-core tasks
4. in under condition 3, if the task is full, but also to reach the maximum number of threads, the task will be rejected, calling
rejectedeExecution RejectedExecutionHandler to inform the caller

Created with Executors factory methods are based on his thread pool to build. Divided into four categories in
1.FixedThreadPool
name suggests, is a fixed number of threads in the thread pool
Write pictures described here
to create a way to newFiedThreadPool (). I can see that corePoolSize , maximumPoolSize settings are equal, which is the core thread and the maximum number of threads equal, meaning It is
only the core thread, and there is no timeout limit, a new future and also perform tasks will be saved to a queue
until there is a free thread, so that it can quickly respond to external requests. Create and destroy overhead is small, but the
amount of concurrency is limited.
2.CachedThreadPool
2.CachedThreadPool
can be seen from the number of kernel threads parameter is zero, but the maximum number of threads is Integer.MAX_VALUE, in the actual
development can be considered infinite, and it can be seen as SynChronousQueue task queue can not be saved,
he will receive direct delivery of mission execution, mean that the thread pool will be a task to create threads join line
drive pool, but he would check before if there is idle threads, and not because of overtime to be recycled, if there is to
reuse, not only to create new, so this thread pool suitable for large and task execution time is very short.
3.ScheduledThreadPool
Write pictures described here
newScheduledThreadPool end up calling the constructor, the internal configuration of the call parent class constructor, nuclear
timeout time is not long, an idle thread will soon be recovered this pool can be repeated periodically according to any delay or
Service can also be executed after the delay period.
4.SingleThreadPool
Write pictures described here
can see a core thread, but also a maximum number of threads, after all, he is only a line
drive thread pool, common thread that he and any difference, his meaning is that you can perform serial multiple tasks, no
need to deal with multithreading issues, but not easy to do continuous common thread to the task, but one by one after the execution.

Guess you like

Origin blog.csdn.net/One_Month/article/details/81509674