面试必考——线程池源码解析之线程池的状态
2021-01-29 08:17
标签:mic shutdown min cap UNC 大于 线程 word dem 线程池状态转换如下: 各运行状态的表示方式:线程池的状态
// runState is stored in the high-order bits
private static final int RUNNING = -1 private static final int SHUTDOWN = 0 private static final int STOP = 1 private static final int TIDYING = 2 private static final int TERMINATED = 3
线程池的是不是看上去很乱?不知道从何下手?
复习位运算符
正数的表示方式
- 计算机中,不管正数,还是负数都用二进制补码表示。
- 正数的原码、反码、补码都一样。
负数的表示方式
原码:一个整数,按照绝对值大小转换成的二进制数
以-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 1;
这三个属性的含义:
- ctl包含两部分信息:workerCount-有效的工作线程数;runState-线程池的状态
- COUNT_BITS:29
- CAPACITY:工作线程的最大容量即00011111111111111111111111111111
private static final int RUNNING = -1 private static final int SHUTDOWN = 0 private static final int STOP = 1 private static final int TIDYING = 2 private static final int TERMINATED = 3 // 11100000000000000000000000000000
private static final int RUNNING = -1 // 00000000000000000000000000000000
private static final int SHUTDOWN = 0 // 00100000000000000000000000000000
private static final int STOP = 1 // 01000000000000000000000000000000
private static final int TIDYING = 2 // 01100000000000000000000000000000
private static final int TERMINATED = 3
面试题——线程池运行状态怎么表示的?
线程池的运行状态保存在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
*/
private static boolean runStateAtLeast(int c, int s) {
return c >= s;
}
/*
* 判断线程池是否处于运行中
*/
private static boolean isRunning(int c) {
return c
为什么根据ctl值就可以算出线程池的状态?
// 11100000000000000000000000000000
private static final int RUNNING = -1 // 00000000000000000000000000000000
private static final int SHUTDOWN = 0 // 00100000000000000000000000000000
private static final int STOP = 1 // 01000000000000000000000000000000
private static final int TIDYING = 2 // 01100000000000000000000000000000
private static final int TERMINATED = 3
- RUNNING
- SHUTDOWN=0
- STOP,TIDYING,TERMINATED大于0
RUNNING
修改工作线程数
/**
* 尝试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()));
}
推荐:七台河SEO
面试必考——线程池源码解析之线程池的状态
标签:mic shutdown min cap UNC 大于 线程 word dem
原文地址:https://www.cnblogs.com/vwvwvwgwg/p/12833424.html