Java thread pool seven arguments detailed

Java thread pool seven arguments detailed

When the multi-threaded Java developers often use the thread pool, this article is a detailed explanation of the seven parameters when creating java thread pool.

 

 

 

As can be seen from the source, the constructor of the thread pool has seven parameters, namely corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler. The following seven parameters will explain everything.

A, corePoolSize core thread pool thread size

Thread pool maintains a minimum number of threads, even if the thread processing idle, they will not be destroyed unless a allowCoreThreadTimeOut. The minimum number of threads here that is corePoolSize.

Two, maximumPoolSize thread pool maximum number of threads

After a task is submitted to the thread pool will first cache to work queue (it will be introduced later), if the work queue is full, a new thread is created, and then handed over to the new thread taken from a work queue task to handle , but the task has just submitted into the work queue. Thread pool will not be open-ended to create a new thread, it will have a maximum limit to the number of threads, the number that is specified by the maximunPoolSize.

Three, keepAliveTime idle thread survival time

If a thread is in an idle state, and the current number of threads is greater than corePoolSize, then after a specified time, the idle thread will be destroyed, where the designated time set by keepAliveTime

Four, unit space thread survival time unit

keepAliveTime units of measurement

Five, workQueue work queue

After the new task is submitted, it will first work to enter into this queue, and then remove the job from the queue when scheduling. jdk provides four work queue:

①ArrayBlockingQueue

Array-based bounded blocking queue, sorted by FIFO. After the new tasks come in, will be placed in the tail of the queue, bounded arrays can prevent resource depletion problem. When the number of threads in the thread pool reaches corePoolSize, and then there are new tasks come in, it will be the task into the tail of the queue, waiting to be scheduled. If the queue is already full, then create a new thread if the number of threads have reached maxPoolSize, it will refuse to execute strategy.

②LinkedBlockingQuene

Based on the list of unbounded blocking queue (in fact, a maximum capacity of Interger.MAX), in FIFO order. Since the approximate unboundedness, when the number of threads in the thread pool reaches corePoolSize, and then there are new tasks come in, it would have been stored in the queue, and not to create a new thread until maxPoolSize, so use the work queue for the queue, the parameters maxPoolSize in fact, it does not work.

③SynchronousQuene

A task not cache blocking queue, the producers put a task must wait until the consumer out this task. That is the new task comes in, will not be cached, but directly is scheduled to perform this task, if there is no available threads, create new threads, if the number of threads reaches maxPoolSize, refused to execute strategy.

④PriorityBlockingQueue

Unbounded blocking queue with priority, the priority is achieved by parameter Comparator.

Six, threadFactory thread factory

The factory to use when creating a new thread, the thread can be used to set the name, whether as a daemon threads, etc.

Seven, handler deny policy

When the work of the task queue has reached the maximum limit, and the number of threads in the thread pool has reached the maximum limit, then if there is a new job submission come in, how to handle it. Here deny policy, it is to solve this problem, jdk provided 4 denial strategy:

①CallerRunsPolicy

Under this policy, the implementation of the run method is rejected task directly in the calling thread unless the thread pool has been shutdown, directly abandon the task.

 

②AbortPolicy

Under this policy, it discards the task and throw RejectedExecutionException exception.

 

 ③DiscardPolicy

Under this policy, it discards the task, doing nothing.

④DiscardOldestPolicy

 

Under this policy, abandoned the queue to enter the first task, then try to reject this task in the queue

 

 By :https://blog.csdn.net/ye17186/article/details/89467919

Follow-up will be detailed supplement!

 

 

 

As can be seen from the source, the constructor of the thread pool has seven parameters, namely corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler. The following seven parameters will explain everything.
A, corePoolSize core thread pool thread size
thread pool maintains a minimum number of threads, even if the thread processing idle, they will not be destroyed unless a allowCoreThreadTimeOut. The minimum number of threads here that is corePoolSize.
Two, maximumPoolSize thread pool maximum number of threads
after a job is submitted to the thread pool will first cache to work queue (will be introduced later), if the work queue is full, it will create a new thread, and then removed from the work queue a new thread to handle the task handed over, but the task has just submitted into the work queue. Thread pool will not be open-ended to create a new thread, it will have a maximum limit to the number of threads, the number that is specified by the maximunPoolSize.
Three, keepAliveTime idle thread survival time of
a thread if the idle state, and the current number of threads is greater than corePoolSize, then after a specified time, the idle thread will be destroyed by the specified time here keepAliveTime set
four, unit space thread survival time unit
keepAliveTime unit of measurement
five, workQueue job queue
after a new task is submitted, it will first work to enter into this queue, and then remove the job from the queue when scheduling. jdk provides four work queue:
①ArrayBlockingQueue
Array-based bounded blocking queue, sorted by FIFO. After the new tasks come in, will be placed in the tail of the queue, bounded arrays can prevent resource depletion problem. When the number of threads in the thread pool reaches corePoolSize, and then there are new tasks come in, it will be the task into the tail of the queue, waiting to be scheduled. If the queue is already full, then create a new thread if the number of threads have reached maxPoolSize, it will refuse to execute strategy.
②LinkedBlockingQuene
blocking queue based on unbounded list of (in fact a maximum capacity of Interger.MAX), in FIFO order. Since the approximate unboundedness, when the number of threads in the thread pool reaches corePoolSize, and then there are new tasks come in, it would have been stored in the queue, and not to create a new thread until maxPoolSize, so use the work queue for the queue, the parameters maxPoolSize in fact, it does not work.
③SynchronousQuene
a task not cache blocking queue, the producers put a task must wait until the consumer out this task. That is the new task comes in, will not be cached, but directly is scheduled to perform this task, if there is no available threads, create new threads, if the number of threads reaches maxPoolSize, refused to execute strategy.
④PriorityBlockingQueue
unbounded blocking queue having a priority, the priority parameter is achieved by Comparator.
Six, threadFactory thread factory
factory use when creating a new thread, the thread can be used to set the name, whether as a daemon threads, etc.
seven, handler deny policy
when the work queue has reached the maximum limit of tasks and threads in the pool the number has reached the maximum limit, then if there is a new job submission come in, how to handle it. Here deny policy, is to solve this problem, jdk provided 4 denial strategy:
①CallerRunsPolicy
Under this policy, the implementation of the run method is rejected task directly in the calling thread unless the thread pool has been shutdown, directly abandon the task. ---------------- Disclaimer: This article is CSDN blogger "ye17186 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. . Original link: https: //blog.csdn.net/ye17186/article/details/89467919

Guess you like

Origin www.cnblogs.com/alex-xyl/p/12460679.html