Classification and usage scenarios of thread pools

newCachedThreadPool:

Bottom layer: return ThreadPoolExecutor instance, corePoolSize is 0; maximumPoolSize is Integer.MAX_VALUE; keepAliveTime is 60L; unit is TimeUnit.SECONDS; workQueue is SynchronousQueue (synchronous queue)
Popular: When a new task arrives, it is inserted into the SynchronousQueue. Since the SynchronousQueue is a synchronous queue, it will find an available thread in the pool to execute it. If there is a thread available, it will be executed. If there is no available thread, a thread will be created to execute the task. ; If the idle time of the thread in the pool exceeds the specified size, the thread will be destroyed.
Applicable: Execute many short-term asynchronous small programs or lightly loaded servers
newFixedThreadPool:

Bottom layer: returns the ThreadPoolExecutor instance, the receiving parameter is the set number of threads nThread, corePoolSize is nThread, maximumPoolSize is nThread; keepAliveTime is 0L (unlimited time); unit is: TimeUnit.MILLISECONDS; WorkQueue is: new LinkedBlockingQueue<Runnable>() None unblock queue
Popular: Create a pool that can accommodate a fixed number of threads. The survival time of each thread is unlimited. When the pool is full, no threads will be added. If all threads in the pool are busy, new tasks will enter the blocking queue. (unbounded blocking queue)
Applicable: perform long-term tasks, the performance is much better
newSingleThreadExecutor:

Bottom layer: ThreadPoolExecutor instance wrapped by FinalizableDelegatedExecutorService, corePoolSize is 1; maximumPoolSize is 1; keepAliveTime is 0L; unit is: TimeUnit.MILLISECONDS; workQueue is: new LinkedBlockingQueue<Runnable>() No unblocking queue
Popular: create a thread pool with only one thread, and the thread's survival time is infinite; when the thread is busy, it will enter the blocking queue for new tasks (unbounded blocking queue)
Applicable: scenarios where one task is executed one task at a time
NewScheduledThreadPool:

Bottom layer: Create ScheduledThreadPoolExecutor instance, corePoolSize is the passed parameter, maximumPoolSize is Integer.MAX_VALUE; keepAliveTime is 0; unit is: TimeUnit.NANOSECONDS; workQueue is: new DelayedWorkQueue() A queue sorted in ascending order of timeout time
Popular: Create a fixed-size thread pool. The thread pool has unlimited survival time. The thread pool can support timing and periodic task execution. If all threads are busy, new tasks will enter the DelayedWorkQueue queue. This is a kind of Queue structure sorted by timeout
Applicable: Scenarios where tasks are executed periodically
Thread pool task execution process:

When the thread pool is smaller than corePoolSize, the newly submitted task will create a new thread to execute the task, even if there are idle threads in the thread pool at this time.
When the thread pool reaches corePoolSize, the newly submitted task will be put into the workQueue, waiting for the task scheduling execution in the thread pool
When the workQueue is full and the maximumPoolSize>corePoolSize, the newly submitted task will create a new thread to execute the task
When the number of submitted tasks exceeds maximumPoolSize, the newly submitted tasks are processed by RejectedExecutionHandler
When the thread pool exceeds corePoolSize threads and the idle time reaches keepAliveTime, close the idle thread
When allowCoreThreadTimeOut(true) is set, the corePoolSize thread in the thread pool will also be closed when the idle time reaches keepAliveTime
Remark:

Generally, if the thread pool task queue adopts the LinkedBlockingQueue queue, then no task will be rejected (because the queue size is not limited). In this case, the ThreadPoolExecutor will only create threads according to the minimum number of threads at most, that is to say, the thread pool size is ignored. .

If the thread pool task queue uses the ArrayBlockingQueue queue, then the ThreadPoolExecutor will adopt a very responsible algorithm, such as assuming that the minimum number of threads in the thread pool is 4, and the maximum number of ArrayBlockingQueue used is 10. As tasks arrive and are put on the queue, a maximum of 4 threads (i.e. the minimum number of threads) run in the thread pool. Even if the queue is completely full, i.e. there are 10 tasks waiting, the ThreadPoolExecutor will only utilize 4 threads. If the queue is full and a new task comes in, a new thread will be started at this time. The task will not be rejected because the queue is full, but a new thread will be started instead. The new thread will run the first task in the queue to make room for the incoming task.

The idea behind this algorithm is that the pool only uses core threads (4) most of the time, even if there are moderate tasks waiting in the queue to run. At this point the thread pool can be used as a throttle. If the squeezing request becomes very high, then the pool will try to run more threads to clean up; this is when the second throttle - the maximum number of threads comes into play.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325873949&siteId=291194637