标签:cut 运算 nat 异或 基础上 work amp aci jvm
基础:
例子: JDK中源码中的线程池ThreadPoolExecutor里面使用了一个int字段来代表了线程池的状态和线程池的数量。
private static final int COUNT_BITS = Integer.SIZE - 3; // 29
private static final int CAPACITY = (1 << COUNT_BITS) - 1; // 高3位0,低29位为1
// runState: 高3位代表线程池的状态; 低29位代表线程的数量(线程数最多为(2^29)-1 )
private static final int RUNNING = -1 << COUNT_BITS; //11100000 00000000 00000000 00000000
private static final int SHUTDOWN = 0 << COUNT_BITS; //00000000 00000000 00000000 00000000
private static final int STOP = 1 << COUNT_BITS; //00100000 00000000 00000000 00000000
private static final int TIDYING = 2 << COUNT_BITS; //01000000 00000000 00000000 00000000
private static final int TERMINATED = 3 << COUNT_BITS; //01100000 00000000 00000000 00000000
private static int runStateOf(int c) {
return c & ~CAPACITY; // 通过取反、然后与运算,可以判断出线程池目前是什么状态
}
private static int workerCountOf(int c) {
return c & CAPACITY; //通过与运算可以算出线程数
}
标签:cut 运算 nat 异或 基础上 work amp aci jvm
原文地址:https://www.cnblogs.com/henuzyy/p/10228978.html