线程池状态转换如下:
各运行状态的表示方式:
// 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;
线程池的是不是看上去很乱?不知道从何下手?
原码:一个整数,按照绝对值大小转换成的二进制数
以-1为例 其原码为:按照绝对值大小转换成的二进制数 10000000000000000000000000000001
-1的反码为:符号位不变,按位取反 11111111111111111111111111111110
-1的补码为:反码加1 11111111111111111111111111111111
怎么证明我们算的是否正确??
/**
* @Author: 无双老师【云析学院:http://yunxiedu.net QQ:3190976240 email:zhouguanya20@163.com】
* @Date: 2020-05-01 20:37
* @Description: 二进制算法
*/
public class BinaryDemo {
public static void main(String[] args) {
System.out.println("-1在计算机中的存储方式为:");
System.out.println(Integer.toBinaryString(-1));
}
}
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;
这三个属性的含义:
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;
---------------------------------------------------------------------
// 11100000000000000000000000000000
private static final int RUNNING = -1 << COUNT_BITS;
// 00000000000000000000000000000000
private static final int SHUTDOWN = 0 << COUNT_BITS;
// 00100000000000000000000000000000
private static final int STOP = 1 << COUNT_BITS;
// 01000000000000000000000000000000
private static final int TIDYING = 2 << COUNT_BITS;
// 01100000000000000000000000000000
private static final int TERMINATED = 3 << COUNT_BITS;
面试题——线程池运行状态怎么表示的?
线程池的运行状态保存在ctl的高3位中
ctl的封装和解封
/*
* 入参c即ctl对应的inValue值
* ~CAPACITY即CAPACITY按位取反即:
* 11100000000000000000000000000000
* c & ~CAPACITY等价于保留c的高3位——线程池的运行状态
*/
private static int runStateOf(int c) { return c & ~CAPACITY; }
/*
* 入参c即ctl对应的inValue值
* CAPACITY即
* 00011111111111111111111111111111
* c & CAPACITY等价于保留c的低29位——线程池的线程数量
*/
private static int workerCountOf(int c) { return c & CAPACITY; }
/*
* 封装线程池状态rs和工作线程数wc到变量ctl中,
* 返回的int值即ctl对应的value值
*/
private static int ctlOf(int rs, int wc) { return rs | wc; }
/*
* 判断线程池状态是否小于s
*/
private static boolean runStateLessThan(int c, int s) {
return c < s;
}
/*
* 判断线程池状态大于等于s
*/
private static boolean runStateAtLeast(int c, int s) {
return c >= s;
}
/*
* 判断线程池是否处于运行中
*/
private static boolean isRunning(int c) {
return c < SHUTDOWN;
}
为什么根据ctl值就可以算出线程池的状态?
// 11100000000000000000000000000000
private static final int RUNNING = -1 << COUNT_BITS;
// 00000000000000000000000000000000
private static final int SHUTDOWN = 0 << COUNT_BITS;
// 00100000000000000000000000000000
private static final int STOP = 1 << COUNT_BITS;
// 01000000000000000000000000000000
private static final int TIDYING = 2 << COUNT_BITS;
// 01100000000000000000000000000000
private static final int TERMINATED = 3 << COUNT_BITS;
RUNNING < SHUTDOWN < STOP < TIDYING < TERMINATED
/**
* 尝试CAS方式将工作线程数加1
*/
private boolean compareAndIncrementWorkerCount(int expect) {
return ctl.compareAndSet(expect, expect + 1);
}
/**
* 尝试CAS方式将工作线程数减1
*/
private boolean compareAndDecrementWorkerCount(int expect) {
return ctl.compareAndSet(expect, expect - 1);
}
/**
* 尝试CAS方式将工作线程数减1
* 仅在线程突然终止时才调用
*/
private void decrementWorkerCount() {
do {} while (! compareAndDecrementWorkerCount(ctl.get()));
}