线程池有几个重要的参数:
maximumPoolSize 最大线程数量,如果新提交的任务因为 workQueue 的容量限制而无法入队,则会尝试新开一个线程执行任务,而如果此时总线程数超过 maximumPoolSize 的限制,那么不再新开一个线程而是提交失败corePoolSize 核心线程数量,当任务执行完毕,线程也将结束它的生命周期,但最少不会低于 corePoolSizekeepAliveTime 空闲线程的存活时间,执行完任务的线程会存活至少 keepAliveTime,再根据当前线程数量和 corePoolSize 决定要不要结束生命workQueue 任务队列,当提交的任务不能被立刻执行时(线程数 > corePoolSize),会放在 workQueue 排队等待执行线程池的内部状态:
RUNNING,可以提交新任务SHUTDOWN,不能提交新任务,但可以继续把 workQueue 里的任务执行完STOP,不能提交新任务,不执行 workQueue 里的任务,且中断正在执行的任务TIDYING,所有任务都已结束,此时线程数为零,准备执行 terminated()TERMINATED,terminated() 执行完毕private final class Worker extends AbstractQueuedSynchronizer implements Runnable {
public void run() {
runWorker(this);
}
}
// worker 的执行流程
// 用一个 while 循环不断地从 workQueue 里获取任务(blocked & timeouted)
// getTask 返回 null 导致当前线程结束生命
void ThreadPoolExecutor.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);
}
}
// 从 workQueue 获取任务(blocked & timeouted)
Runnable ThreadPoolExecutor.getTask() {
boolean timedOut = false; // Did the last poll() time out?
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// 1. 线程池已 shutdown(只需把 workQueue 执行完毕),但 workQueue 已清空
// 2. 线程池已 stop,无需执行 workQueue 剩下的任务
// 此时返回 null 结束线程
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
decrementWorkerCount();
return null;
}
// 当线程数 > corePoolSize,如果阻塞 keepAliveTime 时间段都没有新任务进来,则返回 null 结束当前线程
// 当线程数 > maximumPoolSize 且 workQueue 为空,也要结束当前线程,从而降低线程数
int wc = workerCountOf(c);
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;
}
}
}
生产者 - 消费者模式的 worker
线程(worker)作为消费者,不断地从任务队列(workQueue)里获取任务并执行
void ThreadPoolExecutor.execute(Runnable command) {
// 如果线程数 < corePoolSize,则新开一个线程执行任务
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
// 否则放入 workQueue 等待执行
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
// 如果超过 workQueue 容量限制,则尝试新开一个线程执行任务
// 从下面的 addWorker 可以知道,如果新开线程的时候发现当前线程总数 >= maximumPoolSize,那么任务提交失败
else if (!addWorker(command, false))
reject(command);
}
boolean ThreadPoolExecutor.addWorker(Runnable firstTask, boolean core) {
retry:
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// Check if queue empty only if necessary.
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))
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);
int s = workers.size();
if (s > largestPoolSize)
largestPoolSize = s;
workerAdded = true;
}
} finally {
mainLock.unlock();
}
if (workerAdded) {
t.start();
workerStarted = true;
}
}
} finally {
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
corePoolSize,workQueue 和 maximumPoolSize 之间的关系
定时任务(ScheduledExecutorService)
上文里的任务队列用的是 BlockingQueue,它是按照 FIFO 的优先级给任务排队的;要实现定时,就要按照执行时间点的优先级给任务排序,只有到达执行时间点的任务才能出队
要排序,每次取最小值(到达执行时间点的任务),而且会插入新元素,典型的数据结构是「小顶堆」;DelayedWorkQueue 就是用小顶堆实现的阻塞队列
堆有几个特性: