For more details about the thread pool threadpoolexecutor, refer:
Http://blog.csdn.net/waterbig/article/details/4794214
Http://blog.csdn.net/waterbig/article/details/4794244
Java thread pool (threadpoolexecutor) Source Code Analysis
1 JDK built-in Thread Pool
The thread pool class is Java. util. Concurrent. threadpoolexecutor. The common constructor is as follows:
Threadpoolexecutor(IntCorepoolsize, IntMaximumpoolsize,
LongKeepAliveTime, TimeunitUnit,
Blockingqueue <runnable>Workqueue,
RejectedexecutionhandlerHandler)
Corepoolsize: Minimum number of thread pool maintenance threads
Maximumpoolsize: Maximum number of thread pool maintenance threads
KeepAliveTime: The idle time allowed by the thread pool maintenance thread
Unit: The unit of idle time allowed by the thread pool maintenance thread
Workqueue: Buffer Queue used by the thread pool
Handler: Processing policy of the thread pool to reject tasks
A task is added to the thread pool using the execute (runnable) method. A task is a runnable object. The execution method of a task is
The run () method of the runnable type object.
When a task is added to the thread pool through the execute (runnable) method:
L if the number of threads in the thread pool is smaller than corepoolsize at this time, a new thread should be created to process the added tasks even if all threads in the thread pool are idle.
L if the number in the thread pool is equal to corepoolsize, but the Buffer Queue workqueue is not full, the task is put into the buffer queue.
L if the number in the thread pool isGreater than or equalCorepoolsize: The Buffer Queue workqueue is full, and the number of thread pools is smaller than maximumpoolsize. A new thread is created to process the added tasks.
L if the number in the thread pool is greater than corepoolsize, the Buffer Queue workqueue is full, and the number in the thread pool is equal to maximumpoolsize
The policy specified by handler to process this task. That is, the processing task priority is: Core Thread corepoolsize, task queue workqueue, maximumpoolsize. If all three are full, handler is used to process the rejected task.
L when the number of threads in the thread pool is greater than corepoolsize, if the idle time of a thread exceeds KeepAliveTime, the thread will be terminated. In this way, the thread pool can dynamically adjust the number of threads in the pool.
2. Specific analysis
The preceding description is not abstract, so we can use code to verify whether the thread pool is strictly executed according to the preceding policies:
First, implementRunnableThreadpooltask class, in order to achieve the actual effect of the thread pool, we add a 2 second delay to the run execution body. The Code is as follows:
PublicclassThreadpooltaskImplementsrunnable,Serializable
{
PrivateobjectAttachdata;
Threadpooltask(ObjectTasks)
{
This. Attachdata = tasks;
}
Publicvoidrun(){
System.Out.Println("START task execution :"
+ Attachdata );
Attachdata =Null;
Try{
Thread.Sleep(2000); // The latency is 2 seconds. The demo thread pool is full.
}Catch(InterruptedexceptionE)
{
E.Printstacktrace();
}
}
Publicobjectgettask(){
Returnthis. Attachdata;
}
}
ØVerifyIf the number in the thread pool is equal to corepoolsize, but the Buffer Queue workqueue is not full, the task is put into the buffer queue.
Execution result:
ØVerifyIf the number in the thread pool isGreater than or equalCorepoolsize: The Buffer Queue workqueue is full, and the number of thread pools is smaller than maximumpoolsize. A new thread is created to process the added task.
Execution result:
ØVerifyIf the number in the thread pool is greater than corepoolsize, the Buffer Queue workqueue is full, and the number in the thread pool is equal to maximumpoolsize
The policy specified by handler to process this task. That is, the processing task priority is: Core Thread corepoolsize, task queue workqueue, maximumpoolsize. If all three are full, handler is used to process the rejected task.
Execution result:
Add the verification Handler Registration Policy:
Execution result: