Detailed explanation of the parameters of the java thread pool

Thread pool technology is often used in java multi-threaded development. This article is a detailed explanation of the seven parameters when creating a java thread pool.
Insert picture description here

It can be seen from the source code that the constructor of the thread pool has 7 parameters, which are corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, and handler. The 7 parameters will be explained one by one below.

parameter name meaning Pay attention to the points
corePoolSize The number of core threads, the minimum number of threads maintained in the thread pool (including the number of active and idle threads) 1. After the thread pool is created, it is not the default number of threads of corePoolSize that will be created by default, but the thread will be created only when the task is submitted
maximumPoolSize Maximum number of threads, the maximum number of threads allowed to be created in the thread pool 1. MaximumPoolSize is generally greater than the value of corePoolSize
2. If maximumPoolSize = corePoolSize, then the thread pool is a fixed size thread pool
keepAliveTime The idle thread keep-alive time in the thread pool 1. By default (allowCoreThreadTimeOut = false), keepAliveTime will work only when the number of running threads in the thread pool exceeds corePoolSize, that is, keepAliveTime only works on threads that exceed corePoolSize (for example: corePoolSize = 10, There are currently 15 threads running. At this time, the number of running threads exceeds the number of core threads. When the threads are idle after performing tasks, when the time reaches the critical value of keepAliveTime, 5 threads will be destroyed, and the last only in the thread pool There will be 10 available threads)
2. If the thread pool is set to allowCoreThreadTimeOut = true, then keepAliveTime also has an effect on the core thread, that is, when the thread in the core thread is idle, it will be destroyed when the time reaches the critical value of keepAliveTime. The number of threads in the thread pool can be 0
unit Keep-alive time unit (for example: seconds, milliseconds, etc.) TimeUnit.DAYS; // days
TimeUnit.HOURS; // hours
TimeUnit.MINUTES; // minutes
TimeUnit.SECONDS; // seconds
TimeUnit.MILLISECONDS; // milliseconds
TimeUnit.MICROSECONDS; // microseconds
TimeUnit.NANOSECONDS; // nano second
workQueue Task blocking queue, when the number of tasks exceeds the number of core threads in the current thread pool, the cut queue is not full, and the tasks that need to be executed will be placed in the blocking queue first for waiting After the new task is submitted, it will enter the work queue first, and then take the task out of the queue when the task is scheduled. Four work queues are provided in jdk: see below for details
threadFactory Thread factory object, used to create a new thread instance The factory used when creating a new thread can be used to set the thread name, whether it is a daemon thread, etc.
handler rejection strategy Refusal to execute processing strategy When the tasks in the work queue have reached the maximum limit, and the number of threads in the thread pool has also reached the maximum limit, at this time, if a new task is submitted, how to deal with it. The rejection strategy here is to solve this problem. There are 4 rejection strategies in jdk: see below for details

workQueue work
queue①ArrayBlockingQueue

Bounded blocking queue based on array, sorted by FIFO. After new tasks come in, they will be placed at the end of the queue. A bounded array can prevent resource exhaustion. When the number of threads in the thread pool reaches corePoolSize, and a new task comes in, the task will be placed at the end of the queue, waiting to be scheduled. If the queue is already full, a new thread is created, and if the number of threads has reached maxPoolSize, the rejection strategy will be executed.

②LinkedBlockingQuene

The unbounded blocking queue based on the linked list (in fact, the maximum capacity is Interger.MAX), sorted by FIFO. Due to the approximate unbounded nature of the queue, when the number of threads in the thread pool reaches corePoolSize, new tasks will always be stored in the queue instead of creating new threads until maxPoolSize. Therefore, when using the work queue, the parameter maxPoolSize In fact, it doesn't work.

③SynchronousQuene

A blocking queue that does not cache tasks, the producer puts a task and must wait until the consumer takes out the task. That is to say, when a new task comes in, it will not be cached, but will be directly scheduled to execute the task. If there are no available threads, a new thread will be created. If the number of threads reaches maxPoolSize, the rejection strategy will be executed.

④PriorityBlockingQueue

Unbounded blocking queue with priority, priority is achieved through the parameter Comparator (the queue is rarely used).

Rejection processing policy guidelines:
1. CallerRunsPolicy Under this policy, the run method of the rejected task is directly executed in the caller thread, unless the thread pool has been shut down, the task is directly abandoned. If the task addition fails and the thread pool is not closed, then the calling thread (main thread) will call the execute() method in the executor to execute the task.
2. AbortPolicy The default policy of the thread pool, if there are tasks added If it fails, the task will be discarded and a RejectedExecutionException will be thrown.
3. DiscardPolicy If a task fails to be added, the task will be discarded without any exception being thrown.
4. DiscardOldestPolicy If the task fails to be added, poll() will be removed from the earliest task in the queue and try to add again. If it still fails, it will continue to retry according to this strategy.
5. Customize the policy policy if the above strategy If the policy cannot meet the requirements, you can customize the policy that meets the scenario; implement the rejectedExecution() method in the RejectedExecutorHandler interface.

The source of the article refers to
https://blog.csdn.net/ye17186/article/details/89467919
https://blog.csdn.net/u012253957/article/details/102967238?utm_medium=distribute.pc_relevant.none-task-blog- baidujs_baidulandingword-1&spm=1001.2101.3001.4242

Guess you like

Origin blog.csdn.net/weixin_44887276/article/details/115162666