ThreadPoolExecutor框架是java concurrent包下一种线程池实现框架,而通常我们所用的通过Executors创建线程池的方法,便是对此框架进行封装,通过创建TreadPoolExecutor时上送不同的参数而获取到不同类型的线程池。下面我们来分析一下它的源码具体实现。
首先我们来了解下线程池的状态有哪些:
// 运行中,正在处理任务且可以接受新任务
RUNNING: Accept new tasks and process queued tasks
// 关闭,不接受新任务但是将把队列中剩余任务执行完,当调用了shutdown()方法,会从 RUNNING -> SHUTDOWN
SHUTDOWN: Don't accept new tasks, but process queued tasks
// 终止,不接受新任务不处理队列中剩余任务且停止处理正在执行的任务,当调用了shutdownNow()方法,会从 RUNNING/SHUTDOWN -> STOP
STOP: Don't accept new tasks, don't process queued tasks,
and interrupt in-progress tasks
// 所有任务都已执行完毕,工作线程为0,此时将调用terminated()方法
TIDYING: All tasks have terminated, workerCount is zero,
the thread transitioning to state TIDYING
will run the terminated() hook method
// 线程池中所有线程已经停止运行
TERMINATED: terminated() has completed
然后再来看看ThreadPoolExecutor的一些重要成员变量:
private final BlockingQueue<Runnable> workQueue; //任务缓存队列,用来保存等待中的任务,等待worker线程空闲时执行任务
private final ReentrantLock mainLock = new ReentrantLock(); //更新 poolSize, corePoolSize,maximumPoolSize, runState, and workers set 时需要持有这个锁
private final HashSet<Worker> workers = new HashSet<Worker>(); //用来保存工作中的执行线程集合
private volatile long keepAliveTime; //超过corePoolSize外的线程空闲存活时间
private volatile boolean allowCoreThreadTimeOut; //是否对corePoolSize内的线程设置空闲存活时间
private volatile int corePoolSize; //核心线程数
private volatile int maximumPoolSize; //最大线程数(当线程数已达到核心线程数,且任务队列已满时线程池将会创建新线程,直至线程数达到最大线程数)
private volatile int poolSize; //线程池中的当前线程数
private volatile RejectedExecutionHandler handler; //任务拒绝策略(当线程池已到最大线程数,且任务等待队列已满,仍有任务添加进来时会抛出exception,可设置此handler对任务进行再次提交)
private volatile ThreadFactory threadFactory; //线程工厂,用来新建线程
private int largestPoolSize; //记录线程池中出现过的最大线程数大小
private long completedTaskCount; //已经执行完的线程数
再看ThreadPoolExecutor构造方法,需要传入一些上面传入的重要成员变量完成对象创建:
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
// 若核心线程数小于0、最大线程数小于0,最大线程数小于核心线程数,存活时间小于0均抛出异常
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;
}
再来看看ThreadPoolExecutor的具体使用,调用execute方法时的处理:
public void execute(Runnable command) {
// 提交的任务不可为null
if (command == null)
throw new NullPointerException();
// 若当前工作线程小于核心线程数,则直接新建线程,将任务交由新建线程处理,execute方法返回
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
// 判断若线程池仍为running状态且成功将任务放入等待队列则进入双重检查逻辑
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
// 再次检查线程池是否为running状态,若非且将任务从等待队列移除成功,则将任务交由任务拒绝handler处理
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
// 增加工作线程,若失败则交由任务拒绝处理器处理
else if (!addWorker(command, false))
reject(command);
}
那线程池究竟是怎么新建工作线程的呢?让我们来分析下addWorker这个方法:
private boolean addWorker(Runnable firstTask, boolean core) {
retry:
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// 若线程池未关闭且等待队列不为空则返回false(需先入等待队列)
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
// 若增加核心线程且核心现车已到最大值,或增加普通线程且普通线程已到最大值,则均不增加work,返回false
for (;;) {
int wc = workerCountOf(c);
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
// 采用CAS乐观锁修改当前工作线程数,若成功则跳出retry进入下面新增worker逻辑
if (compareAndIncrementWorkerCount(c))
break retry;
// 由于CAS修改工作线程数失败,证明工作线程数已被修改需要重新获取,然后跳转到retry重复此循环逻辑
c = ctl.get(); // Re-read ctl
if (runStateOf(c) != rs)
continue retry;
// else CAS failed due to workerCount change; retry inner loop
}
}
boolean workerStarted = false;
boolean workerAdded = false;
Worker w = null;
try {
// 构造一个新worker,worker实现了Runnable方法
w = new Worker(firstTask);
// 此线程为线程池的线程构造工厂制造
final Thread t = w.thread;
if (t != null) {
final ReentrantLock mainLock = this.mainLock;
// 增加工作线程工作需加锁
mainLock.lock();
try {
// Recheck while holding lock.
// Back out on ThreadFactory failure or if
// shut down before lock acquired.
// 获取锁后再检查若线程池已关闭或传入任务为null则抛出异常
int rs = runStateOf(ctl.get());
if (rs < SHUTDOWN ||
(rs == SHUTDOWN && firstTask == null)) {
if (t.isAlive()) // precheck that t is startable
throw new IllegalThreadStateException();
// 将新建worker放入hashSet工作线程集合中
workers.add(w);
// 判断若此时worker数量大于曾经出现的最大线程数,则更新此最大线程数
int s = workers.size();
if (s > largestPoolSize)
largestPoolSize = s;
workerAdded = true;
}
} finally {
// 最后别忘了解锁
mainLock.unlock();
}
// 增加工作线程成功后,将此新线程启动
if (workerAdded) {
t.start();
workerStarted = true;
}
}
} finally {
// 若此worker线程启动失败,则做增加线程失败处理,将此worker从workers集合中移除
if (! workerStarted)
addWorkerFailed(w);
}
// 最后返回工作线程是否已启动标识
return workerStarted;
}
至此增加工作线程已完成并已完成启动,那么启动后worker又是如何工作的呢?我们可以来看看Worker这个内部类,这个内部类实现了Runnable接口的run方法,因此工作线程将会调用此run方法完成任务,此run方法又调用了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 {
// 重要:此处循环检查任务不为空进入处理,先处理完addWorker时添加的任务,然后调用getTask去任务队列里边取任务
// 直到任务队列里边无等待任务才跳出循环,以此实现线程复用
while (task != null || (task = getTask()) != null) {
w.lock();
// If pool is stopping, ensure thread is interrupted;
// if not, ensure thread is not interrupted. This
// requires a recheck in second case to deal with
// shutdownNow race while clearing interrupt
// 检查线程池是否已关闭,若关闭则将线程中断,若没有则开始任务
if ((runStateAtLeast(ctl.get(), STOP) ||
(Thread.interrupted() &&
runStateAtLeast(ctl.get(), STOP))) &&
!wt.isInterrupted())
wt.interrupt();
try {
// 空方法,可加入执行任务前逻辑
beforeExecute(wt, task);
Throwable thrown = null;
try {
// 执行任务
task.run();
} catch (RuntimeException x) {
thrown = x; throw x;
} catch (Error x) {
thrown = x; throw x;
} catch (Throwable x) {
thrown = x; throw new Error(x);
} finally {
// 空方法,可加入执行任务后逻辑
afterExecute(task, thrown);
}
} finally {
// 将task置null,将完成任务数+1,解锁
task = null;
w.completedTasks++;
w.unlock();
}
}
// 是否被中断标识置为false
completedAbruptly = false;
} finally {
// 处理完成后结束进程,并视现存线程数和任务队列任务数情况而定再新增worker
processWorkerExit(w, completedAbruptly);
}
}
由此我们可以看到ThreadPoolExecutor框架的处理流程为:
1、任务提交时若线程数未达到CorePoolSize则创建新线程处理任务;
2、任务提交时若线程数已达到CorePoolSize则将任务存入任务缓存队列
3、若线程已达到CorePoolSize且任务队列已满,当又有任务提交时,则创建新线程处理直至线程总数达到MaxPoolSize
4、若线程池未被shutDown,则线程处理完最初提交的任务后会一直从等待队列获取任务直到任务队列为空,然后停止