ThreadPoolExecutor
里面 有这些状态
* The runState provides the main lifecycle control, taking on values:
*
* RUNNING: Accept new tasks and process queued tasks
* SHUTDOWN: Don't accept new tasks, but process queued tasks
* STOP: Don't accept new tasks, don't process queued tasks,
* and interrupt in-progress tasks
* TIDYING: All tasks have terminated, workerCount is zero,
* the thread transitioning to state TIDYING
* will run the terminated() hook method
* TERMINATED: terminated() has completed
状态设计机制
* The main pool control state, ctl, is an atomic integer packing
* two conceptual fields
* workerCount, indicating the effective number of threads
* runState, indicating whether running, shutting down etc
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
一个整形 存2值: 状态 线程数
前3bit是状态
111: RUNNING
000: SHUTDOWN
001: STOP
010: TIDYING
011: TERMINATED
private static final int RUNNING = -1 << COUNT_BITS;
private static final int SHUTDOWN = 0 << COUNT_BITS;
private static final int STOP = 1 << COUNT_BITS;
private static final int TIDYING = 2 << COUNT_BITS;
private static final int TERMINATED = 3 << COUNT_BITS;
public class ThreadPoolStateTest {
//ctl是线程池中一个非常重要的变量,以它的低29位表示线程池中处于RUNNING状态的线程个数,高3位表示线程池所处的状态
public static int COUNT_BITS = Integer.SIZE - 3;
public static int CAPACITY = (1 << COUNT_BITS) - 1;
public static void main(String[] args) {
// <<表示 左移
int RUNNING = -1 << COUNT_BITS;
int SHUTDOWN = 0 << COUNT_BITS;
int STOP = 1 << COUNT_BITS;
int TIDYING = 2 << COUNT_BITS;
int TERMINATED = 3 << COUNT_BITS;
System.out.println("Integer.SIZE is :"+Integer.SIZE+" , COUNT_BITS is : "+COUNT_BITS);
System.out.println("CAPACITY is : "+CAPACITY+" , "+Integer.toBinaryString(CAPACITY));
//五种状态中SHUTDOWN值等于0,RUNNING值小于0,其他三种状态STOP、TIDYING、TERMINATED值均大于0
System.out.println("RUNNING = " + RUNNING + " = " + Integer.toBinaryString(RUNNING));
System.out.println("SHUTDOWN = " + SHUTDOWN + " = " + Integer.toBinaryString(SHUTDOWN));
System.out.println("STOP = " + STOP + " = 00" + Integer.toBinaryString(STOP));
System.out.println("TIDYING = " + TIDYING + " = 0" + Integer.toBinaryString(TIDYING));
System.out.println("TERMINATED = " + TERMINATED + " = 0" + Integer.toBinaryString(TERMINATED));
}
//CAPACITY低29位全1高3位全0,它与c做与运算得到的就是当前运行的线程个数
static int workerCountOf(int c) {
return c & CAPACITY;
}
}
Integer.SIZE is :32 , COUNT_BITS is : 29
CAPACITY is : 536870911 , 11111111111111111111111111111
RUNNING = -536870912 = 11100000000000000000000000000000
SHUTDOWN = 0 = 0
STOP = 536870912 = 00100000000000000000000000000000
TIDYING = 1073741824 = 01000000000000000000000000000000
TERMINATED = 1610612736 = 01100000000000000000000000000000
Process finished with exit code 0
之间的切换
* RUNNING -> SHUTDOWN
* On invocation of shutdown(), perhaps implicitly in finalize()
* (RUNNING or SHUTDOWN) -> STOP
* On invocation of shutdownNow()
* SHUTDOWN -> TIDYING
* When both queue and pool are empty
* STOP -> TIDYING
* When pool is empty
* TIDYING -> TERMINATED
* When the terminated() hook method has completed
3个事件, 把生命分成4段,SHUTDOWN和STOP 取其一
- 1.关闭线程池
- 2.任务都结束
- 3.
terminated()
方法执行完
网友评论