The core argument of the thread pool and the implementation process

Thread Pool

Thread pool type

newFixedThreadPool

Creates a thread pool that reuses a fixed number of threads to a shared unbounded queue to run these threads. At any point in the large
majority nThreads threads will be active processing tasks. If you submit additional task when all threads are active,
then there are threads available before, additional tasks will wait in the queue. If during the execution before the closure due to a failure of any
thread terminates, then replace it with a new thread will execute subsequent tasks (if needed). In a thread is closed it explicitly
before, the thread pool will always exist.

newCachedThreadPool

You may need to create a thread pool that creates new threads, but they will reuse previously constructed threads when available. For execution
in terms of a lot of short-term program asynchronous tasks, these pools will typically improve program performance. Calls to execute will reuse previously constructed
threads (if the thread is available). If an existing thread is not available, a new thread is created and added to the pool. Terminate and
remove those threads that have not been used for 60 seconds from the cache. Therefore, for a long time remain idle thread pool will not use any information
source.

newScheduledThreadPool

Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.

newSingleThreadExecutor

(Or when an exception occurs) returns only one thread pool thread, the thread pool thread can be restarted after the death of a thread to replace the original thread to continue execution!

Core parameters

corePoolSize

The core number of threads, this parameter indicates the number of threads to keep the core in the thread pool, defined by their own, just create a thread pool, which is the core number of threads 0, with the addition of the task, reaching the number of kernel threads, executing the task after that, the core number of threads in the thread pool which has remained at the core number of threads set.

maximumPoolSize

The maximum number of threads This parameter defines a maximum number of threads in the thread pool can hold. When a job is submitted to the thread pool, if the number of threads reaches the core number of threads, and the task queue is full, the task can no longer queue when adding a task, then checks whether the task reached the maximum number of threads, if not reached , create new threads, tasks, otherwise, reject the policy.

keepAliveTime

(Maximum number of threads - the core number of threads) thread survival time is greater than the core number of threads in the thread pool that part of the thread, that thread count of the total number minus the number of threads that part of the core thread, after performing tasks in the thread pool survival time.

workQueue

Task queue, used for cushioning effect, when the number of tasks submitted to the core threads in the thread pool is not enough, all of the core threads are executed task, the task will be saved to the task queue, waiting for the core thread to execute. The main parameter is the number of kernel threads are performing their duties, only works of parameters, mainly used to cache task.

Process

flow chart

Here Insert Picture Description

Process of combing

Users submit jobs to see if the core thread pool is full, not full then create threads, tasks,
full to the buffer queue, the queue buffer is not full, then the task is added to the queue is full,
determine whether the maximum thread pool is full , did not create full thread, mission, strategy is denied full

Released eight original articles · won praise 0 · Views 216

Guess you like

Origin blog.csdn.net/weixin_44048746/article/details/104484857