execute
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
// 拿到当前线程池的state
int c = ctl.get();
// 如果工作线程数小于核心线程数
if (workerCountOf(c) < corePoolSize) {
// 那么添加工作线程,并执行
if (addWorker(command, true))
return;
// 更新state
c = ctl.get();
}
// 如果添加工作线程失败或者工作线程已经等于或超过了核心线程数,那么将任务放到队列中
if (isRunning(c) && workQueue.offer(command)) {
// 再次更新state
int recheck = ctl.get();
// 如果当前线程池状态异常,那么执行reject策略
if (! isRunning(recheck) && remove(command))
reject(command);
// 如果队列中有任务,且当前工作线程为0
// 那么新建一个空的工作者,它会去队列拿任务去执行
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
// 到这里,说明超过了核心线程数,队列也加不进,可能满了
// 但是还没有超过最大线程数,那么这里直接去开新的工作线程进行执行,因为也总不能丢掉吧。
// 当如addWorker失败,那么执行reject策略
else if (!addWorker(command, false))
reject(command);
}
addWorker
private boolean addWorker(Runnable firstTask, boolean core) {
retry:
for (;;) {
int c = ctl.get();
// 通过ctl拿到线程池运行状态
int rs = runStateOf(c);
// Check if queue empty only if necessary.
// 首先如果线程池状态是STOP,TIDYING,TERMINATED的话,那么就不再接收新的任务了,直接返回
// 其次如果线程池状态是SHUTDOWN,但传入的任务不为null的话,那么就不再接收新的任务了,直接返回
// 再次如果线程池状态是SHUTDOWN,但队列中已经没有任务了,那么就不再接收新的任务了,直接返回
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
// 自旋
for (;;) {
// 拿到现在线程池的工作线程数
int wc = workerCountOf(c);
// 如果线程数大于极值(2^29-1)或分情况大于核心或最大线程数,那么返回
// 表示不再接收新的任务
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
// 否则工作线程数加一,如果失败,那么说明有竞争,跳出外循环retry
if (compareAndIncrementWorkerCount(c))
break retry;
// 更新ctl
c = ctl.get(); // Re-read ctl
// 如果线程池状态变化,那么重新开始外循环retry
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
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());
// 如果状态时RUNNING或是SHUTDOWN且传入的任务为空
if (rs < SHUTDOWN ||
(rs == SHUTDOWN && firstTask == null)) {
// 按道理在这里t还不应该启动,这里如果已经start,直接抛异常
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) {
// 执行Worker的runWorker
t.start();
workerStarted = true;
}
}
} finally {
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
runWorker
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
// TODO 为什么要unlock
w.unlock(); // allow interrupts
boolean completedAbruptly = true;
try {
// 如果当前worker是否带有任务,如果没带,去队列领取任务
// getTask会阻塞等待新的任务,最不济也是有等待超时
// 正常来说while会一直自旋,然后不停的执行一个又一个,直到没有任务可执行为止
while (task != null || (task = getTask()) != null) {
// 锁定worker,表示在工作中
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
// 这里保证在STOP/TIDYING/TERMINATED时,当前执行线程中断
// 同时也保证在RUNNING/SHUTDOWN,当前执行线程不是中断状态
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 {
// 完成任务,且计数+1,释放worker
task = null;
w.completedTasks++;
w.unlock();
}
}
// 正常来讲,worker一直到没有任务可执行为止,或者等待超时
// completedAbruptly=false,表示正常结束
// 如果是true,一定是任务执行有异常,提前结束了
completedAbruptly = false;
} finally {
processWorkerExit(w, completedAbruptly);
}
}
processWorkerExit
private void processWorkerExit(Worker w, boolean completedAbruptly) {
// 如果是true,代表任务执行异常,那么工作线程数减一,以便回收
if (completedAbruptly) // If abrupt, then workerCount wasn't adjusted
decrementWorkerCount();
final ReentrantLock mainLock = this.mainLock;
// 锁定线程池
mainLock.lock();
try {
// 将worker的任务完成数加总到线程池的总数上
completedTaskCount += w.completedTasks;
// 移除该worker
workers.remove(w);
} finally {
mainLock.unlock();
}
// 根据线程池状态,是否需要终止线程池
tryTerminate();
int c = ctl.get();
// 如果是RUNNING或SHUTDOWN状态
if (runStateLessThan(c, STOP)) {
// 如果worker是正常死亡,如果工作线程个数大于等于核心线程数,那么直接返回,因为worker够用
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);
}
}
getTask
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.
// 如果线程池状态时STOP/TIDYING/TERMINATED
// 或者是SHUTDOWN,且任务队列为空
// 那么工作线程数减一,待回收,返回空
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
decrementWorkerCount();
return null;
}
// 拿到当前工作线程数
int wc = workerCountOf(c);
// Are workers subject to culling?
// 核心线程除非设置了allowCoreThreadTimeOut,不会进行超时回收
// 如果是非核心线程则需要超时处理
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
// 如果线程数超过了最大线程池数,当然要回收worker
// 如果是非核心线程且任务队列为空,说明僧多粥少,回收worker
// 如果是核心线程需要超时回收,且任务队列也没任务,那么也要回收worker
if ((wc > maximumPoolSize || (timed && timedOut))
&& (wc > 1 || workQueue.isEmpty())) {
// 如果worker数目能成功减一,那么直接返回null
// 否则,自旋,下次再来
if (compareAndDecrementWorkerCount(c))
return null;
continue;
}
try {
// 根据是否需要超时处理来从任务队列中获取任务
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
// 如果拿到,那么返回该任务
if (r != null)
return r;
// 到这里说明已经超时,不然会一直block到任务为止
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
reject
AbortPolicy
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
// 直接拒绝执行,抛出RejectedExecutionException
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
DiscardPolicy
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
// 直接忽略
}
DiscardOldestPolicy
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
// 丢弃掉最早的任务,执行现在这个任务
e.getQueue().poll();
e.execute(r);
}
}
CallerRunsPolicy
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
// 不再丢给线程池去执行,直接在主线程执行
r.run();
}
}
shutdown
public void shutdown() {
final ReentrantLock mainLock = this.mainLock;
// 线程池加锁
mainLock.lock();
try {
checkShutdownAccess();
// 切换线程池状态为SHUTDOWN
advanceRunState(SHUTDOWN);
// 遍历工作线程,中断闲置的woker
interruptIdleWorkers();
// 钩子,子类自己定义
onShutdown(); // hook for ScheduledThreadPoolExecutor
} finally {
mainLock.unlock();
}
tryTerminate();
}
interruptIdleWorkers
private void interruptIdleWorkers(boolean onlyOne) {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
// 遍历工作线程,中断闲置的woker
for (Worker w : workers) {
Thread t = w.thread;
if (!t.isInterrupted() && w.tryLock()) {
try {
t.interrupt();
} catch (SecurityException ignore) {
} finally {
w.unlock();
}
}
if (onlyOne)
break;
}
} finally {
mainLock.unlock();
}
}
tryTerminate
final void tryTerminate() {
for (;;) {
int c = ctl.get();
// 如果线程池的状态是RUNNING,还在运行,不允许中止线程池
// 如果线程池的状态是/TIDYING/TERMINATED,已经中止过了
// 如果线程池的状态是SHUTDOWN且任务队列不为空,还有任务没有执行完,不允许中止
// 以上满足其一,直接返回。
if (isRunning(c) ||
runStateAtLeast(c, TIDYING) ||
(runStateOf(c) == SHUTDOWN && ! workQueue.isEmpty()))
return;
// 只有SHUTDOWN状态且任务队列为空,或者 STOP状态能执行到这一步
// 说明要不就是有工作线程在执行任务,要不就是工作线程在等待任务,那么中断其中一个空闲的工作线程
if (workerCountOf(c) != 0) { // Eligible to terminate
// 并返回
interruptIdleWorkers(ONLY_ONE);
return;
}
// 如果状态是SHUTDOWN且任务队列为空且正在运行的工作线程也没有了,那么开始terminated
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
// 将线程池的状态设置为TIDYING
if (ctl.compareAndSet(c, ctlOf(TIDYING, 0))) {
try {
// 执行钩子,子类定义
terminated();
} finally {
// 最终将线程池的状态设置为TERMINATED
ctl.set(ctlOf(TERMINATED, 0));
// 然后唤醒所有等待在termination条件下的线程
termination.signalAll();
}
return;
}
} finally {
mainLock.unlock();
}
// else retry on failed CAS
}
}
shutdownNow
public List shutdownNow() {
List tasks;
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
// 设置线程池状态为STOP
advanceRunState(STOP);
// 中断所有的运行或等待任务也就是空闲的工作线程
interruptWorkers();
// 将未执行完的任务转移给tasks返回
tasks = drainQueue();
} finally {
mainLock.unlock();
}
// 尝试中止线程池
tryTerminate();
return tasks;
}