上一篇已经介绍完了ThreadPoolExecutor结构体系,也对FutureTask做了详细的分析,这次将对ThreadPoolExecutor源码进行详细分析,作为线程池的核心实现类,面试必问的考察点之一,学习多线程无论如何都要把这个类给掌握了。
// 一个int存两个数:线程池工作线程数(后29位)+线程池状态(前3位)。
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;
// 线程池的状态
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 final BlockingQueue<Runnable> workQueue;
// 存放线程池中所有的工作线程
private final HashSet<Worker> workers = new HashSet<Worker>();
// 线程池达到的最大线程数,用来记录的,小于等于maximumPoolSize
private int largestPoolSize;
// 完成任务数量
private long completedTaskCount;
// 创建工作线程的工厂
private volatile ThreadFactory threadFactory;
// 拒绝任务处理器(提供4种处理器,也可以自己实现,默认抛异常)
private volatile RejectedExecutionHandler handler;
// 空闲线程等待工作的超时时间
private volatile long keepAliveTime;
// 如果为false(默认),则即使处于空闲状态,核心线程也保持活动状态。
// 如果为true,则核心线程使用keepAliveTime来超时等待工作
private volatile boolean allowCoreThreadTimeOut;
// 核心线程数
private volatile int corePoolSize;
// 线程池最大线程数,这个需要我们设置的
// 作用:为了增强线程池的弹性工作。
private volatile int maximumPoolSize;
// 获取线程池状态
private static int runStateOf(int c) {
return c & ~CAPACITY; }
// 获取工作线程数
private static int workerCountOf(int c) {
return c & CAPACITY; }
// 两个数组合成一个数
private static int ctlOf(int rs, int wc) {
return rs | wc; }
ThreadPoolExecutor有4个构造方法,但最终都是调用一个构造方法,所以这里只介绍这一个构造方法。
corePoolSize:核心线程数大小
maximumPoolSize:最大线程数大小
keepAliveTime:空闲存活时间
unit:时间单位
workQueue:存放任务的阻塞队列
threadFactory:创建工作线程的工厂
handler:拒绝策略处理器
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;
}
前面看到属性中有Worker中,这个类继承了AQS,简单重写了独占模式对应的方法,实现了Runnable接口。Worker封装了工作线程,也封装了任务,因为Worker实现了Runnable接口,则可以把自己交给线程去执行。
private final class Worker extends AbstractQueuedSynchronizer implements Runnable
{
/** 工作线程*/
final Thread thread;
/** 需要完成的初始化任务,可能为null */
Runnable firstTask;
/** 当前线程完成任务计数器 */
volatile long completedTasks;
Worker(Runnable firstTask) {
setState(-1); // runWorker之前禁止中断
this.firstTask = firstTask;
this.thread = getThreadFactory().newThread(this); // 创建一个新线程,该线程会执行this.run方法。
}
public void run() {
runWorker(this);
}
protected boolean isHeldExclusively() {
return getState() != 0;
}
protected boolean tryAcquire(int unused) {
if (compareAndSetState(0, 1)) {
setExclusiveOwnerThread(Thread.currentThread());
return true;
}
return false;
}
protected boolean tryRelease(int unused) {
setExclusiveOwnerThread(null);
setState(0);
return true;
}
public void lock() {
acquire(1); }
public boolean tryLock() {
return tryAcquire(1); }
public void unlock() {
release(1); }
public boolean isLocked() {
return isHeldExclusively(); }
void interruptIfStarted() {
Thread t;
if (getState() >= 0 && (t = thread) != null && !t.isInterrupted()) {
try {
t.interrupt();
} catch (SecurityException ignore) {
}
}
}
}
执行大致逻辑:
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true)) // 将command作为第一个任务开启一个核心线程处理
return;
c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) {
// 加入阻塞队列中
int recheck = ctl.get();// double-check
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)// 如果没有工作线程了新建一个
addWorker(null, false); // 只是单纯创建一个普通线程
}
else if (!addWorker(command, false)) // 将command作为第一个任务开启一个普通线程处理
reject(command);
}
添加工作线程,并执行任务firstTask。
firstTask:第一个任务
core:是否为核心线程
private boolean addWorker(Runnable firstTask, boolean core) {
retry:
for (;;) {
// 将线程数量+1
int c = ctl.get();
int rs = runStateOf(c);
// 检查线程池的状态
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
for (;;) {
int wc = workerCountOf(c);
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
if (compareAndIncrementWorkerCount(c)) // 只有这里为true才会退出外层for循环
break 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 {
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.
int rs = runStateOf(ctl.get());
if (rs < SHUTDOWN ||
(rs == SHUTDOWN && firstTask == null)) {
if (t.isAlive()) // precheck that t is startable
throw new IllegalThreadStateException();
workers.add(w); // 将w添加到集合中
int s = workers.size();
if (s > largestPoolSize)
largestPoolSize = s;
workerAdded = true;
}
} finally {
mainLock.unlock();
}
if (workerAdded) {
t.start(); // 添加成功后就执行任务,调用t.start方法为什么后面会执行worker.run方法呢?答案就在创建t的时候,可以回过去看看
workerStarted = true;
}
}
} finally {
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
执行提交给线程池的任务,如果当前worker没有任务,则去队列中取任务执行。如果任务都执行完了,则处理worker退出,即线程数量减1,把当前worker工作的任务数汇总,然后从worker集合中删除。
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 (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;
w.completedTasks++;
w.unlock();
}
}
completedAbruptly = false;
} finally {
processWorkerExit(w, completedAbruptly);// 处理worker退出
}
}
getTask()
从等待队列中获取任务,如果是核心线程那就一直等待任务直到获取成功,如果是非核心线程则等待对应时间,如果还没有获取到任务则将worker数量减1并返回null。
private Runnable getTask() {
boolean timedOut = false; // Did the last poll() time out?
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// Check if queue empty only if necessary.
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
decrementWorkerCount();
return null;
}
int wc = workerCountOf(c);
// Are workers subject to culling?
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
if ((wc > maximumPoolSize || (timed && timedOut))
&& (wc > 1 || workQueue.isEmpty())) {
if (compareAndDecrementWorkerCount(c))
return null;
continue;
}
try {
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
processWorkerExit(Worker w, boolean completedAbruptly)
处理worker的退出,结算该worker处理的任务数量进行汇总,然后从worker集合中移除。对于worker数量采取少补策略。
private void processWorkerExit(Worker w, boolean completedAbruptly) {
if (completedAbruptly) // If abrupt, then workerCount wasn't adjusted
decrementWorkerCount();
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
completedTaskCount += w.completedTasks;
workers.remove(w);
} finally {
mainLock.unlock();
}
tryTerminate();
int c = ctl.get();
if (runStateLessThan(c, STOP)) {
if (!completedAbruptly) {
int min = allowCoreThreadTimeOut ? 0 : corePoolSize;
if (min == 0 && ! workQueue.isEmpty())
min = 1;
if (workerCountOf(c) >= min)
return; // replacement not needed
}
addWorker(null, false);
}
}