ThreadPoolExecutor里有这段代码:
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
private static final int COUNT_BITS = Integer.SIZE - 3;
private static final int CAPACITY = (1 << COUNT_BITS) - 1;
// runState is stored in the high-order bits
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;
// Packing and unpacking ctl
private static int runStateOf(int c) { return c & ~CAPACITY; }
private static int workerCountOf(int c) { return c & CAPACITY; }
private static int ctlOf(int rs, int wc) { return rs | wc; }
COUNT_BITS:32位-3位=29位
高三位 | 低29位 | 含义 | |
CAPACITY | 000 | 11111111111111111111111111111 | 容量,最大线程数 |
RUNNING | -001 | 00000000000000000000000000000 | 线程状态 |
SHUTDOWN | 000 | 00000000000000000000000000000 | 线程状态 |
STOP | 001 | 00000000000000000000000000000 | 线程状态 |
TIDYING | 010 | 00000000000000000000000000000 | 线程状态 |
TERMINATED | 011 | 00000000000000000000000000000 | 线程状态 |