ThreadPoolExecutor源码解析
ThreadPoolExecutor源码解析 1、常用变量的解释 // 1. `ctl`,可以看做一个int类型的数字,高3位表示线程池状态,低29位表示worker数量 private final AtomicInteger ctl = new AtomicInteger (ctlOf( RUNNING , 0 )); // 2. `COUNT_BITS`,`Integer.SIZE`为32,所以`COUNT_BITS`为29 private static final int COUNT_BITS = Integer . SIZE - 3 ; // 3. `CAPACITY`,线程池允许的最大线程数。1左移29位,然后减1,即为 2^29 - 1 private static final int CAPACITY = ( 1 << COUNT_BITS ) - 1 ; // runState is stored in the high-order bits // 4. 线程池有5种状态,按大小排序如下:RUNNING < SHUTDOWN < STOP < TIDYING < TERMINATED private static final int RUNNING = - 1 << COUNT_BITS ; private static final int SHUTDOWN = 0