private void processWorkerExit(Worker w, boolean completedAbruptly) {
if (completedAbruptly) // If abrupt, then workerCount wasn't adjusted
//这个worker不能正常工作,worker数量减一
//这个worker是正常工作的,但是现在没有任务了,要退出了,数量就不减1了?
decrementWorkerCount();
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
//汇总该worker(一个worker实际上就是一个线程)完成的任务数量到线程池
completedTaskCount += w.completedTasks;
workers.remove(w);
} finally {
mainLock.unlock();
}
//尝试着去Terminated一下线程池
tryTerminate();
int c = ctl.get();
//线程池的状态是runing 或者 shutdown
if (runStateLessThan(c, STOP)) {
//是因为没有任务了才要终止这个线程的
if (!completedAbruptly) {
//假如allowCoreThreadTimeOut是false,那么线程池中的线程数量至少是corePoolSize
//假如allowCoreThreadTimeOut是true,那么一旦没有任务了,所有线程就都终止了
int min = allowCoreThreadTimeOut ? 0 : corePoolSize;
if (min == 0 && ! workQueue.isEmpty())
min = 1;
//目前正在工作的线程数量大于等于min,则正常结束这个线程(就是维持线程数量至少是min)
if (workerCountOf(c) >= min)
return; // replacement not needed
}
//增加一个线程
addWorker(null, false);
}
}
网友评论