前置工作
一般我们使用的java线程池的大多都是由ThreadPoolExecutor所生成的。
类结构图如下:
在说源码前,先说明几个概念,这几个概念不弄明白,那源码也很难看懂。
首先是线程池的状态和数量在线程池中是如何表示的
ThreadPoolExecutor 使用 int 的高 3 位来表示线程池状态,低 29 位表示线程数量
在线程池中使用了一个AtomicInteger类型的变量来保存线程池的状态和数量
不妨看看源码是如何定义的:
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));//初始值为-1
private static final int RUNNING = -1 << COUNT_BITS;//RUNNING转换成二进制
//就是11100000000000000000000000000000,如果只看前面三位的话RUNNING的值就是-1
private static final int COUNT_BITS = Integer.SIZE - 3;//COUNT_BITS==29
private static int ctlOf(int rs, int wc) { return rs | wc; }
这个应该还是很好理解的吧,经过计算ctlOf(RUNNING, 0),初始化的ctl其实就是11100000000000000000000000000000,这个值表示的是线程池为RUNNING状态,0条工作线程。再举个例子:11100000000000000000000000000011,表示RUNNING状态,3条工作线程。
ctl的几个状态:
//下面的值只显示其二进制的前三位
private static final int RUNNING = -1 << COUNT_BITS;//111
private static final int SHUTDOWN = 0 << COUNT_BITS;//000
private static final int STOP = 1 << COUNT_BITS;//001
private static final int TIDYING = 2 << COUNT_BITS;//010
private static final int TERMINATED = 3 << COUNT_BITS;//011
//其中
private static final int COUNT_BITS = Integer.SIZE - 3;//29
好了,接下来就是最有意思的源码环节了😏
源码分析
首先从构造器出发
线程池的构造器:
public ThreadPoolExecutor(int corePoolSize,//核心线程数目 (最多保留的线程数)
int maximumPoolSize,//最大线程数目
long keepAliveTime,//生存时间 - 针对救急线程
TimeUnit unit,//时间单位 - 针对救急线程
BlockingQueue<Runnable> workQueue,//阻塞队列
ThreadFactory threadFactory,//线程工厂 - 可以为线程创建时起个好名字
RejectedExecutionHandler handler) {//拒绝策略
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
通过这个构造器,我们就可以创建我们想要的线程池。
构造器有几个参数
BlockingQueue:这是一个阻塞队列,如果这个不懂,可以先去了解一下。
篇幅关系,在这就详细不说明了。
ThreadFactory :线程工厂
就拿DefaultThreadFactory 举例
static class DefaultThreadFactory implements ThreadFactory { private static final AtomicInteger poolNumber = new AtomicInteger(1); private final ThreadGroup group; private final AtomicInteger threadNumber = new AtomicInteger(1); private final String namePrefix;
<span class="token comment">//构造方法</span> <span class="token function">DefaultThreadFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> SecurityManager s <span class="token operator">=</span> System<span class="token punctuation">.</span><span class="token function">getSecurityManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取线程组</span> group <span class="token operator">=</span> <span class="token punctuation">(</span>s <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token operator">?</span> s<span class="token punctuation">.</span><span class="token function">getThreadGroup</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getThreadGroup</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//线程名称</span> namePrefix <span class="token operator">=</span> <span class="token string">"pool-"</span> <span class="token operator">+</span> poolNumber<span class="token punctuation">.</span><span class="token function">getAndIncrement</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"-thread-"</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//创建线程</span> <span class="token keyword">public</span> Thread <span class="token function">newThread</span><span class="token punctuation">(</span>Runnable r<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> Thread t <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>group<span class="token punctuation">,</span> r<span class="token punctuation">,</span> namePrefix <span class="token operator">+</span> threadNumber<span class="token punctuation">.</span><span class="token function">getAndIncrement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//设置线程为非守护线程</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>t<span class="token punctuation">.</span><span class="token function">isDaemon</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> t<span class="token punctuation">.</span><span class="token function">setDaemon</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//设置线程的优先级,其中Thread.NORM_PRIORITY是5(优先级的范围是1~10)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>t<span class="token punctuation">.</span><span class="token function">getPriority</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> Thread<span class="token punctuation">.</span>NORM_PRIORITY<span class="token punctuation">)</span> t<span class="token punctuation">.</span><span class="token function">setPriority</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span>NORM_PRIORITY<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> t<span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
其实本质上就是创建一个Thread,只不过多了一些参数设置
RejectedExecutionHandler:拒绝策略,阻塞队列满了且线程数量达到maximumPoolSize会采取对应的拒绝策略
拒绝策略,jdk默认实现有4种,拿一个举例
public static class AbortPolicy implements RejectedExecutionHandler {
<span class="token keyword">public</span> <span class="token function">AbortPolicy</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token punctuation">}</span> <span class="token comment">//直接抛异常</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">rejectedExecution</span><span class="token punctuation">(</span>Runnable r<span class="token punctuation">,</span> ThreadPoolExecutor e<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RejectedExecutionException</span><span class="token punctuation">(</span><span class="token string">"Task "</span> <span class="token operator">+</span> r<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">" rejected from "</span> <span class="token operator">+</span> e<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
上面就是构造器的介绍
接下来是线程池的任务提交,这也是最核心的方法了
//当我们提交一个任务时所调用的方法 public void execute(Runnable command) { //任务为空 if (command == null) throw new NullPointerException();
<span class="token comment">//获取ctl(就是我们刚才介绍的那个原子变量,后面不再提示)</span> <span class="token keyword">int</span> c <span class="token operator">=</span> ctl<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//判断工作中的线程数量<corePoolSize(核心线程数量)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">workerCountOf</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span> <span class="token operator"><</span> corePoolSize<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">/* workerCountOf方法:获取原子变量后面29位的值,也就是工作线程的数量 private static int workerCountOf(int c) { return c & CAPACITY; } private static final int CAPACITY = (1 << COUNT_BITS) - 1; 所以CAPACITY的二进制:00011111111111111111111111111111 */</span> <span class="token comment">//直接将任务添加到工作线程所在的集合中,addWorker()方法后面会有介绍</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">addWorker</span><span class="token punctuation">(</span>command<span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span> c <span class="token operator">=</span> ctl<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//代码能到这里说明工作线程数量>=corePoolSize,那么就尝试添加到阻塞队列中。</span> <span class="token comment">//如果线程池是RUNNING状态并且成功添加到阻塞队列中</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isRunning</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span> <span class="token operator">&&</span> workQueue<span class="token punctuation">.</span><span class="token function">offer</span><span class="token punctuation">(</span>command<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">/* isRunning()方法: private static boolean isRunning(int c) { return c < SHUTDOWN;//ctl状态可以看上面的表格 } */</span> <span class="token comment">//再次获取ctl,在并发环境下,可能之前获取的ctl已经发生改变</span> <span class="token keyword">int</span> recheck <span class="token operator">=</span> ctl<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">/* 如果线程池处于!RUNNING状态,则删除阻塞队列中刚才添加的任务 (只有RUNNING状态才能将任务添加到阻塞队列中) 其实我觉得这块做再次校验的目的是尽量保证当线程池被其它线程shutdown 后,阻塞队列就不能再添加任务了(按理来说shutdown状态,阻塞队列是不能 再添加任务的) 为什么说它是尽量保证呢,因为它也只能保证从第一获取ctl和第二次获取ctl 之间如果ctl发生改变,能及时做出相应的修改 但如果我在第二次获取ctl后,也就是int recheck = ctl.get()执行后, 突然线程池变成了shutdown的状态,那下面的条件就不会成立, 刚刚添加到阻塞队列中的任务就不会被移除了。当然发生这种情况的概率非常 小,即使发生了,无非就是多执行一个任务。 */</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span> <span class="token function">isRunning</span><span class="token punctuation">(</span>recheck<span class="token punctuation">)</span> <span class="token operator">&&</span> <span class="token function">remove</span><span class="token punctuation">(</span>command<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//删除成功则说明任务添加失败,直接走拒接策略</span> <span class="token function">reject</span><span class="token punctuation">(</span>command<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">/* 如果线程池已经停止了(不处于RUNNING状态,但处于SHOTDOWN状态) 判断工作中的线程数量是否为0,为0则创建一个空任务 这个是不是感到很疑惑??????我看了当初也是懵了。 我也是看了后面的源码才焕然大悟:工作中的线程数量为0时,那么就没有线程 去阻塞队列中取任务执行了,那么我们刚才添加到阻塞队列的任务就永远不会 执行。所以我们添加了一个空任务的Worker(Worker可以执行多个任务,后面会说到)。 那为何工作中的线程数量会为0?因为我们可能设置corePoolSize为0(比如 Executors.newCachedThreadPool()),那么所有线程接 在keepAliveTime时间内获取不到阻塞队列中的任务时,就会被回收。最终导 致线程池中工作的线程数量为0 */</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">workerCountOf</span><span class="token punctuation">(</span>recheck<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token function">addWorker</span><span class="token punctuation">(</span>null<span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//上面if条件不成立,则可能是阻塞队列满了,那么我们直接将任务添加到工作线程集</span> <span class="token comment">//合中(工作线程数超过corePoolSize就会将任务添加到阻塞队列中,阻塞队列满了就会</span> <span class="token comment">//再次尝试将任务添加到工作线程中,但也不能超过maximumPoolSize)</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">addWorker</span><span class="token punctuation">(</span>command<span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//超过maximumPoolSize则添加失败,走拒绝策略</span> <span class="token function">reject</span><span class="token punctuation">(</span>command<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
接下来是addWorker()方法:
//firstTask是添加的任务,core来判断是核心线程还是最大线程,即corePoolSize和maximumPoolSize private boolean addWorker(Runnable firstTask, boolean core) { retry: for (;;) { //获取ctl int c = ctl.get(); //获取线程池运行状态 int rs = runStateOf(c);
<span class="token comment">/* 判断线程池是否还在运行 其中rs>=SHUTDOWN表示线程处于非RUNNING状态 ! (rs == SHUTDOWN && firstTask == null && ! workQueue.isEmpty()) 上面这三行条件:是为了保证阻塞队列的任务在SHUTDOWN状态下也能被执行(创建空任务的原因) */</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>rs <span class="token operator">>=</span> SHUTDOWN <span class="token operator">&&</span> <span class="token operator">!</span> <span class="token punctuation">(</span>rs <span class="token operator">==</span> SHUTDOWN <span class="token operator">&&</span> firstTask <span class="token operator">==</span> null <span class="token operator">&&</span> <span class="token operator">!</span> workQueue<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token comment">/* 下面这段带代码的目的是将工作线程的线程数量+1(也就是ctl中的后29位) */</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token punctuation">;</span><span class="token punctuation">;</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//获取工作线程的数量</span> <span class="token keyword">int</span> wc <span class="token operator">=</span> <span class="token function">workerCountOf</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">/* 工作线程数量超过规定大小则返回false 条件1:wc >= CAPACITY 判断工作线程的数量是否超过线程的最大容量 其中CAPACITY的定义:private static final int CAPACITY = (1 << COUNT_BITS) - 1; COUNT_BITS的定义private static final int COUNT_BITS = Integer.SIZE - 3; 经过计算CAPACITY的值为00011111111111111111111111111111 (前面3为用来表示线程状态),经计算,也就是536,870,912 条件2:wc >= (core ? corePoolSize : maximumPoolSize) 判 断core是否为核心线程,core是方法参数 */</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>wc <span class="token operator">>=</span> CAPACITY <span class="token operator">||</span> wc <span class="token operator">>=</span> <span class="token punctuation">(</span>core <span class="token operator">?</span> corePoolSize <span class="token operator">:</span> maximumPoolSize<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token comment">//cas将ctl的值+1(工作线程数+1),并结束外层循环</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">compareAndIncrementWorkerCount</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">break</span> retry<span class="token punctuation">;</span> c <span class="token operator">=</span> ctl<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Re-read ctl</span> <span class="token comment">//上面cas失败,并且两次读取的ctl的运行状态不一致(说明线程池的运行状</span> <span class="token comment">//态发生了改变),则continue外层循环重试</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">runStateOf</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span> <span class="token operator">!=</span> rs<span class="token punctuation">)</span> <span class="token keyword">continue</span> retry<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment">/* 上面代码将ctl中的工作线程数量+1,但只是数量+1,任务还没有被执行。 下面这段代码是将任务添加到工作线程的集合中 */</span> <span class="token keyword">boolean</span> workerStarted <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token keyword">boolean</span> workerAdded <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> Worker w <span class="token operator">=</span> null<span class="token punctuation">;</span><span class="token comment">//这个Worker结构后面会谈及到,先看一下大致结构</span> <span class="token comment">/* private final class Worker extends AbstractQueuedSynchronizer implements Runnable { final Thread thread; Runnable firstTask;//任务 volatile long completedTasks;//完成任务的数量 public void run() { runWorker(this); } } */</span> <span class="token keyword">try</span> <span class="token punctuation">{<!-- --></span> w <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Worker</span><span class="token punctuation">(</span>firstTask<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">final</span> Thread t <span class="token operator">=</span> w<span class="token punctuation">.</span>thread<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>t <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">final</span> ReentrantLock mainLock <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>mainLock<span class="token punctuation">;</span> mainLock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">try</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//获取线程池运行状态</span> <span class="token keyword">int</span> rs <span class="token operator">=</span> <span class="token function">runStateOf</span><span class="token punctuation">(</span>ctl<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">/* rs < SHUTDOWN 判断线程池是否关闭 rs == SHUTDOWN && firstTask == null是为了保证阻塞队列 的任务在SHUTDOWN状态下也能被执行(创建空任务的原因) */</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>rs <span class="token operator"><</span> SHUTDOWN <span class="token operator">||</span> <span class="token punctuation">(</span>rs <span class="token operator">==</span> SHUTDOWN <span class="token operator">&&</span> firstTask <span class="token operator">==</span> null<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">if</span> <span class="token punctuation">(</span>t<span class="token punctuation">.</span><span class="token function">isAlive</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// precheck that t is startable</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">IllegalThreadStateException</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//添加到工作线程的集合中</span> workers<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>w<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> s <span class="token operator">=</span> workers<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>s <span class="token operator">></span> largestPoolSize<span class="token punctuation">)</span> largestPoolSize <span class="token operator">=</span> s<span class="token punctuation">;</span> <span class="token comment">//添加成功后修改添加状态,后面需要</span> workerAdded <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{<!-- --></span> mainLock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//判断任务是否添加成功</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>workerAdded<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//添加成功则启动</span> t<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//这行很重要,后面会说到</span> <span class="token comment">//修改启动标识,后面需要</span> workerStarted <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//判断是否启动</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span> workerStarted<span class="token punctuation">)</span> <span class="token comment">//如果启动失败,则将刚刚添加到工作线程集合中的任务删除</span> <span class="token function">addWorkerFailed</span><span class="token punctuation">(</span>w<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">/* private void addWorkerFailed(Worker w) { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { if (w != null) workers.remove(w);删除工作线程集合中的Worker decrementWorkerCount();工作线程数量-1 tryTerminate();尝试将线程池变成TERMINATED状态 } finally { mainLock.unlock(); } } */</span> <span class="token punctuation">}</span> <span class="token comment">//返回启动状态</span> <span class="token keyword">return</span> workerStarted<span class="token punctuation">;</span> <span class="token punctuation">}</span>
上面我们说完了任务提交的大概流程,但你有没有想过添加到阻塞队列中的任务好像并没有被执行,一个Worker执行完了任务又会怎么样?线程池是怎么做到一个线程可以执行多个任务,而不是执行一个任务就释放一个线程。还有就是线程池的异常处理,按理来说如果一个异常没有被捕获,线程就会挂掉,挂掉后线程池的线程数量就会减小,那么线程池是怎么保证线程的数量的呢。带着问题,我们继续。
在上面代码分析中,我们还去没有仔细去分析Worker这个类,源码来
private final class Worker extends AbstractQueuedSynchronizer implements Runnable {
<span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token keyword">long</span> serialVersionUID <span class="token operator">=</span> <span class="token number">6138294804551838833</span>L<span class="token punctuation">;</span> <span class="token keyword">final</span> Thread thread<span class="token punctuation">;</span> Runnable firstTask<span class="token punctuation">;</span> <span class="token keyword">volatile</span> <span class="token keyword">long</span> completedTasks<span class="token punctuation">;</span> <span class="token function">Worker</span><span class="token punctuation">(</span>Runnable firstTask<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token function">setState</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>firstTask <span class="token operator">=</span> firstTask<span class="token punctuation">;</span><span class="token comment">//任务</span> <span class="token keyword">this</span><span class="token punctuation">.</span>thread <span class="token operator">=</span> <span class="token function">getThreadFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">newThread</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//创建一个线程,</span> <span class="token comment">//注意这里传递的任务不是firstTask ,而是this,因为Worker类也是Runnable的</span> <span class="token comment">//子类,因此线程启动时调用的方法是下面的run方法</span> <span class="token punctuation">}</span> <span class="token comment">//线程启动调用的方法</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token function">runWorker</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
其实这里用到了一个设计模式,装饰器模式(你们注意一下Runnable,实现+组合,这不典型的装饰器模式么)。
可以看出,一个Worker对应一个Thread
还得我们在execute()方法中说很重要的一行代码的t.start()吗?这里调用的其实是Worker中的run方法,不是firstTask的run方法。
因此t.start()也就是执行下面代码
public void run() { runWorker(this); }
我们再进入runWorker()方法
//这里我就不逐行解释了,就说了几行关键部分 final void runWorker(Worker w) { Thread wt = Thread.currentThread(); Runnable task = w.firstTask; w.firstTask = null; w.unlock(); // allow interrupts boolean completedAbruptly = true; try { /* 发现没有,while循环,一个Worker是可以执行多个任务的 task!=null,还记得execute()中的addWorker(null, false)这行代码吗?添加一个空任务 那行代码在这里就起作用了,如果任务为null,那么就会去阻塞队列中取任务 getTask()就是去阻塞队列中获取任务,如果没有任务getTask()会一直阻塞, 除非等待时间超过keepAliveTime或者线程池被终止运行,该方法后面会详细说到 getTask()后面会详细说到 */ while (task != null || (task = getTask()) != null) { w.lock();
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token function">runStateAtLeast</span><span class="token punctuation">(</span>ctl<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> STOP<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">(</span>Thread<span class="token punctuation">.</span><span class="token function">interrupted</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&&</span> <span class="token function">runStateAtLeast</span><span class="token punctuation">(</span>ctl<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> STOP<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">&&</span> <span class="token operator">!</span>wt<span class="token punctuation">.</span><span class="token function">isInterrupted</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> wt<span class="token punctuation">.</span><span class="token function">interrupt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">try</span> <span class="token punctuation">{<!-- --></span> <span class="token function">beforeExecute</span><span class="token punctuation">(</span>wt<span class="token punctuation">,</span> task<span class="token punctuation">)</span><span class="token punctuation">;</span> Throwable thrown <span class="token operator">=</span> null<span class="token punctuation">;</span> <span class="token keyword">try</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//这个任务才是我们之前execute()方法传递过来的任务</span> task<span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">RuntimeException</span> x<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> thrown <span class="token operator">=</span> x<span class="token punctuation">;</span> <span class="token keyword">throw</span> x<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Error</span> x<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> thrown <span class="token operator">=</span> x<span class="token punctuation">;</span> <span class="token keyword">throw</span> x<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Throwable</span> x<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> thrown <span class="token operator">=</span> x<span class="token punctuation">;</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{<!-- --></span> <span class="token function">afterExecute</span><span class="token punctuation">(</span>task<span class="token punctuation">,</span> thrown<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{<!-- --></span> task <span class="token operator">=</span> null<span class="token punctuation">;</span> w<span class="token punctuation">.</span>completedTasks<span class="token operator">++</span><span class="token punctuation">;</span> w<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> completedAbruptly <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//Worker收尾工作,后面会详细说到</span> <span class="token function">processWorkerExit</span><span class="token punctuation">(</span>w<span class="token punctuation">,</span> completedAbruptly<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
有了上面的这个方法,问题差不多解决了一半
我们看看getTask()方法到底做了些什么
private Runnable getTask() {
<span class="token comment">//线程去阻塞队列中获取任务执行,如果阻塞队列一直没有任务,则会超时</span> <span class="token keyword">boolean</span> timedOut <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token comment">// Did the last poll() time out?</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token punctuation">;</span><span class="token punctuation">;</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//获取ctl</span> <span class="token keyword">int</span> c <span class="token operator">=</span> ctl<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取线程池运行状态</span> <span class="token keyword">int</span> rs <span class="token operator">=</span> <span class="token function">runStateOf</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">/* 如果线程池为STOP、TIDYING、TERMINATED这几种状态,则该工作线程应该终止 如果线程为SHUTDOWN状态并且阻塞队列为空,则该工作线程也应该终止 简单说就是如果线程池关闭了,该工作线程就应该终止 */</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>rs <span class="token operator">>=</span> SHUTDOWN <span class="token operator">&&</span> <span class="token punctuation">(</span>rs <span class="token operator">>=</span> STOP <span class="token operator">||</span> workQueue<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//工作线程数量-1</span> <span class="token function">decrementWorkerCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> null<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">int</span> wc <span class="token operator">=</span> <span class="token function">workerCountOf</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">/* allowCoreThreadTimeOut:如果为false(默认值),核心线程即使处于空闲状态也保持活动状态。如果为true,核心线程使用keepAliveTime超时等待工作。 private volatile boolean allowCoreThreadTimeOut; timed可以理解为线程是否能一直处于空闲状态,线程处于空闲状态一段时间后会被回收(默认数量超过corePoolSize的线程需要被回收, 如果设置allowCoreThreadTimeOut为true,那么创建的所有线程都不会被回收) */</span> <span class="token keyword">boolean</span> timed <span class="token operator">=</span> allowCoreThreadTimeOut <span class="token operator">||</span> wc <span class="token operator">></span> corePoolSize<span class="token punctuation">;</span> <span class="token comment">/* 这块代码目地是让那些等待超时的线程被回收 这里的第一个条件: wc > maximumPoolSize 我也不知道开发人员为何要这样写, 因为wc>maximumPoolSize是不可能发生的,addWorker()方法中已经保证了 在并发环境下wc<=maximumPoolSize。 第二个条件:timed && timedOut 目的是保证那些等待超时的线程被回收 第三个条件:wc > 1 || workQueue.isEmpty() 保证线程池中至少有一个 线程活动,除非阻塞队列为空 */</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>wc <span class="token operator">></span> maximumPoolSize <span class="token operator">||</span> <span class="token punctuation">(</span>timed <span class="token operator">&&</span> timedOut<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">&&</span> <span class="token punctuation">(</span>wc <span class="token operator">></span> <span class="token number">1</span> <span class="token operator">||</span> workQueue<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">compareAndDecrementWorkerCount</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> null<span class="token punctuation">;</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">try</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">/* timed为true则线程不能一直处于空闲状态,等待一定时间后还没接收到任 务,那么线程就会被回收 timed为false则线程一直等待阻塞队列中的任务。篇幅比较大,阻塞队列 就不进行说明了。 总之一句话poll()只等待一段的时间,而take()会一直等待 */</span> Runnable r <span class="token operator">=</span> timed <span class="token operator">?</span> workQueue<span class="token punctuation">.</span><span class="token function">poll</span><span class="token punctuation">(</span>keepAliveTime<span class="token punctuation">,</span> TimeUnit<span class="token punctuation">.</span>NANOSECONDS<span class="token punctuation">)</span> <span class="token operator">:</span> workQueue<span class="token punctuation">.</span><span class="token function">take</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//如果r不为null,则说明从阻塞队列中拿到了任务</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>r <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token keyword">return</span> r<span class="token punctuation">;</span> <span class="token comment">//代码如果能走到这里,则r为null,等待超时</span> timedOut <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> retry<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> timedOut <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
最后就是Worker结束所调用的方法
//参数 w:工作线程 completedAbruptly:true表示工作线程异常,false表示工作线程正常执行 private void processWorkerExit(ThreadPoolExecutor.Worker w, boolean completedAbruptly) {
<span class="token comment">//如果是异常退出,则认为任务不计数,因为之前执行了incrementWorkerCount,所</span> <span class="token comment">//以需要执行decrementWorkerCount</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>completedAbruptly<span class="token punctuation">)</span> <span class="token comment">// If abrupt, then workerCount wasn't adjusted</span> <span class="token function">decrementWorkerCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">final</span> ReentrantLock mainLock <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>mainLock<span class="token punctuation">;</span> mainLock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">try</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//线程池完成的总任务</span> completedTaskCount <span class="token operator">+=</span> w<span class="token punctuation">.</span>completedTasks<span class="token punctuation">;</span> <span class="token comment">//删除工作线程集合中的Worker</span> workers<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>w<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{<!-- --></span> mainLock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//尝试将线程池的状态改为TERMINATED</span> <span class="token function">tryTerminate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取ctl</span> <span class="token keyword">int</span> c <span class="token operator">=</span> ctl<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">/* private static boolean runStateLessThan(int c, int s) { return c < s; } 判断运行状态是否为SHUTDOWN或RUNNING */</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">runStateLessThan</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> STOP<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//工作线程正常执行完</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>completedAbruptly<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">/* min表示线程池核心线程数量(你也可以理解成corePoolSize) allowCoreThreadTimeOut:如果为false(默认值),核心线程即使处于 空闲状态也保持活动状态。如果为true,核心线程使用keepAliveTime 超时等待工作。 private volatile boolean allowCoreThreadTimeOut; */</span> <span class="token keyword">int</span> min <span class="token operator">=</span> allowCoreThreadTimeOut <span class="token operator">?</span> <span class="token number">0</span> <span class="token operator">:</span> corePoolSize<span class="token punctuation">;</span> <span class="token comment">//如果阻塞队列中还有任务,则核心线程数量不能为0</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>min <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">&&</span> <span class="token operator">!</span> workQueue<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> min <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">//如果工作线程数量>=min(线程池核心线程数量),则直接返回,否则还会有</span> <span class="token comment">//最后一行代码的执行,因为要保证工作中线程数量>=corePoolSize</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">workerCountOf</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span> <span class="token operator">>=</span> min<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token comment">// replacement not needed</span> <span class="token punctuation">}</span> <span class="token comment">/* 添加一个新线程worker,这里worker的添加有可能是因为workerCountOf(c) >= min, 也有可能是因为上面条件!completedAbruptly==false(工作线程出现异 常,出现异常我们就需要重新创建一个新的worker, 这也就是为何我们使用线程池时不需要考虑因线程异常而导致线程数量减小的原因) */</span> <span class="token function">addWorker</span><span class="token punctuation">(</span>null<span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
ThreadPoolExecutor线程池的工作流程大概就是这样,如果哪里有误欢迎评论指出!