ThreadPoolExecutor 原理解析
本文首发于个人微信公众号《andyqian》,期待你的关注 前言 在上一篇文章《 Java线程池ThreadPoolExecutor 》中描述了ThreadPoolExecutor的基本概念,以及一些常用方法。这对于我们来说,是远远不够的,今天就一起来看TreadPoolExecutor类的内部实现。 线程池状态 在学习ThreadPoolExecutor源码时,首先来看看下面这段代码,也是非常重要的一段代码。 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; // 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;