private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0)); //融合线程池状态+线程数(高四位表示线程状态)
private static final int COUNT_BITS = Integer.SIZE - 3; //29
private static final int CAPACITY = (1 << COUNT_BITS) - 1; //1逻辑左移动29位后减1;用二进制表示则高四位全部为0;低28位全部为1
// runState is stored in the high-order bits
private static final int RUNNING = -1 << COUNT_BITS; //1110 0000 0000 0000 0000 0000 0000 0000
private static final int SHUTDOWN = 0 << COUNT_BITS; //0000 0000 0000 0000 0000 0000 0000 0000
private static final int STOP = 1 << COUNT_BITS; //0010 0000 0000 0000 0000 0000 0000 0000
private static final int TIDYING = 2 << COUNT_BITS; //0100 0000 0000 0000 0000 0000 0000 0000
private static final int TERMINATED = 3 << COUNT_BITS; //0110 0000 0000 0000 0000 0000 0000 0000
// Packing and unpacking ctl
private static int runStateOf(int c) { return c & ~CAPACITY; } //c 为ctl,和CAPACITY位取反后进行与运算;低28位为0,所以线程状态保留,线程数被屏蔽。
private static int workerCountOf(int c) { return c & CAPACITY; } //c 为ctl,和CAPACITY进行与运算;则高四位为0,所以线程池状态被屏蔽;保留的后28位即可得出线程数。
private static int ctlOf(int rs, int wc) { return rs | wc; } //或运算符,将线程池状态和线程数量融合到一个32位置整形数据中。
待分析