Threads have 5 states: new State , ready state , running state , blocking state , dead state . The thread pool also has 5 states; However, threads are different from threads, and the 5 states of the line pool are:RUNNING, SHUTDOWN, STOP, tidying, TERMINATED.
The thread pool status definition code is as follows:
Private FinalAtomicinteger ctl =NewAtomicinteger (Ctlof (RUNNING, 0));Private Static Final intCount_bits = integer.size-3;Private Static Final intCapacity = (1 << count_bits)-1;Private Static Final intRUNNING =-1 <<count_bits;Private Static Final intSHUTDOWN = 0 <<count_bits;Private Static Final intSTOP = 1 <<count_bits;Private Static Final inttidying = 2 <<count_bits;Private Static Final intTERMINATED = 3 <<count_bits;Private Static intCtlof (intRsintWC) {returnRS | wc }
Description :
A CTL is an atomic object of type Atomicinteger. The CTL Records 2 information about the number of tasks in the thread pool and the thread pooling status.
The CTL consists of 32 bits in total. Where the high 3 bits represent the thread pool state, and the low 29 bits represent the number of tasks in the thread pool.
RUNNING -- The corresponding high 3-bit value is 111. SHUTDOWN The high 3-bit value for-- STOP The high 3-bit value for-- tidying The high 3-bit value for-- TERMINATED -the corresponding high 3-bit value is 011.
The switch between the various states of the thread pool is as follows:
1. RUNNING
(01) Status Note: When the thread pool is in the running state, it is able to receive new tasks and process the added tasks.
(02) State Toggle: The initialization state of the thread pool is running. In other words, once the thread pool is created, it is in the running state!
The reason is simple, in the initialization code of the CTL (below), it is initialized to the running state, and "task number" is initialized to 0.
Private Final New Atomicinteger (Ctlof (RUNNING, 0));
2. SHUTDOWN
(01) Status Note: When the thread pool is in the shutdown state, it does not receive new tasks, but can handle the tasks that have been added.
(02) State Toggle: When invoking the SHUTDOWN () interface of the thread pool, the thread pool is running by SHUTDOWN.
3. STOP
(01) Status Description: When the thread pool is in the stop state, it does not receive new tasks, does not process the added tasks, and interrupts the task being processed.
(02) State toggle: When the Shutdownnow () interface of the thread pool is called, the thread pool is (RUNNING or SHUTDOWN), STOP.
4. Tidying
(01) Status Note: When all tasks are terminated, the number of tasks for the CTL record is 0, and the thread pool becomes tidying state. When the thread pool becomes tidying state, the hook function terminated () is executed. Terminated () is empty in the Threadpoolexecutor class and is handled appropriately if the user wants the online pool to be tidying, which can be implemented by overloading the terminated () function.
(02) State toggle: When the thread pool is in the SHUTDOWN state, the blocking queue is empty and the tasks performed in the thread pool are empty, it is SHUTDOWN-tidying.
When the thread pool is in the stop state, the task that is executed in the threads pools is empty, which is caused by stop---tidying.
5. TERMINATED
(01) Status Description: The thread pool is completely terminated and becomes terminated state.
(02) State toggle: When the thread pool is in tidying state, after executing TERMINATED (), it will be TERMINATED by tidying.
The thread pool's deny policy refers to the actions taken when a task is added to the thread pool and is rejected.
When a task is added to the thread pool it is rejected, possibly because: first, the thread pool closes unexpectedly. Second, the number of tasks exceeds the maximum limit of the thread pool.
thread pool consists of 4 rejection policies: , callerrunspolicy , discardoldestpolicy and discardpolicy .
abortpolicy -- When a task is added to the thread pool and is rejected, it throws a Rejectedexecutionexception exception. callerrunspolicy -- discardoldestpolicy -- When a task is added to the thread pool and is rejected, the thread pool discards the oldest unhandled task waiting in the queue. The rejected task is then added to the wait queue. discardpolicy --When a task is added to the thread pool and is rejected, the thread pool discards the rejected task.
The thread pool default processing policy is abortpolicy!
[Http://www.cnblogs.com/skywang12345/p/3512947.html]
[Http://www.cnblogs.com/skywang12345/p/3544116.html]
Thread pool life cycle and rejection policy