码迷,mamicode.com
首页 > 编程语言 > 详细

面试必考——线程池源码解析之线程池的状态

时间:2020-05-05 23:37:13      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:mic   shutdown   min   cap   UNC   大于   线程   word   dem   

线程池的状态

  • RUNNING:接收新的任务,执行阻塞队列中的任务
  • SHUTDOWN:不接受新任务,但是仍然可以处理阻塞队列中的任务
  • STOP:不接受新任务,不处理阻塞队列中的任务,中断正在处理的任务
  • TIDYING:所有任务已终止,workerCount(工作线程数等于0),进入TIDYING状态将会执行terminated()方法
  • TERMINATED:terminated()方法执行完成后进入TERMINATED状态

线程池状态转换如下:

技术图片

各运行状态的表示方式:

// 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. 计算机中,不管正数,还是负数都用二进制补码表示。
  2. 正数的原码、反码、补码都一样。

负数的表示方式

原码:一个整数,按照绝对值大小转换成的二进制数

以-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;

这三个属性的含义:

  1. ctl包含两部分信息:workerCount-有效的工作线程数;runState-线程池的状态
  2. COUNT_BITS:29
  3. CAPACITY:工作线程的最大容量即00011111111111111111111111111111
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<0
  • SHUTDOWN=0
  • STOP,TIDYING,TERMINATED大于0

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()));
}
推荐:七台河SEO

面试必考——线程池源码解析之线程池的状态

标签:mic   shutdown   min   cap   UNC   大于   线程   word   dem   

原文地址:https://www.cnblogs.com/vwvwvwgwg/p/12833424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!