ThreadPoolExecutor principle, usage

ThreadPoolExecutor constructor

java.util.concurrent.ThreadPoolExecutor.ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)

Creates a new ThreadPoolExecutor with the given initial parameters.

Parameters:
corePoolSize the number of threads to keep in the pool, even if they are idle.
maximumPoolSize the maximum number of threads to allow in the pool.
keepAliveTime when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.
unit the time unit for the keepAliveTime argument.
workQueue the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
threadFactory the factory to use when the executor creates a new thread.
handler the handler to use when execution is blocked because the thread bounds and queue capacities are reached.
Throws:
IllegalArgumentException - if corePoolSize, or keepAliveTime less than zero, or if maximumPoolSize less than or equal to zero, or if corePoolSize greater than maximumPoolSize.
NullPointerException - if workQueue or threadFactory or handler are null.

 


 

The working mechanism of ThreadPoolExecutor: 

 

 

The task processing of the entire ThreadPoolExecutor has 4 steps:

 

  • The first step, the initial poolSize < corePoolSize, the submitted runnable task will be directly used as the parameter of a new Thread, executed immediately, and return
  • In the second step, when the number of submitted tasks exceeds the corePoolSize, the second step is entered. Will submit the current runable to a block queue, if the submission is successful, return
  • In the third step, if poolSize < maximumPoolsize, it will try to create a new Thread for emergency treatment, and immediately execute the corresponding runnable task
  • The fourth step, if the add thread is successful and the current task is just scheduled for execution, return
  • Step 5: The add thread fails, reject, otherwise it means that the add thread can still be added , but the task being executed is not the current task, repeat 12345, and try again until the submission is successful or rejected.

                -- In the case where task processing is slow and the queue is full, the number of threads in the thread pool will quickly reach maxPoolSize)

 

Calling execute is enough, no need to call submit()

 

JDK1.5 impl:

 

<!--WizRtf2Html Charset=0 -->public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
for (;;) {
if (runState != RUNNING) {
reject(command);
return;
}
if (poolSize < corePoolSize && addIfUnderCorePoolSize(command))
return;
if (workQueue.offer(command))
return;
Runnable r = addIfUnderMaximumPoolSize(command);
if (r == command)
return;
if (r == null) {
reject(command);
return;
}
// else retry keeps trying until return
}
}
 

 ===================================================

 

ThreadPoolExecutor number of threads

After the executor is constructed, the initial number of threads in the thread pool is 0, and the creation of threads will only be triggered when the task is responded to.

corePoolSize the number of threads to keep in the pool, even if they are idle.

maximumPoolSize the maximum number of threads to allow in the pool.  

 

-----------------------

线程对象只有在线程执行完毕后,才会被回收,所以只要线程池没有被shutdown,则ThreadPoolExecutor无法被回收

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326995198&siteId=291194637