线程池shutdown流程以及线程的销毁
前文: 线程池的execute
流程图
shutdown()
public void shutdown() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
// 检查权限, 是否可以终止线程池
checkShutdownAccess();
// 自旋设置线程池状态为SHUTDOWN
// 在shutdownNow里面这里设置的是STOP, 并且会返回一个Runnable的List
advanceRunState(SHUTDOWN);
// 将workers集合中的所有线程标记为interrupt
// 这里需要获取worker的锁
interruptIdleWorkers();
onShutdown(); // hook for ScheduledThreadPoolExecutor
} finally {
mainLock.unlock();
}
tryTerminate();
}
public List<Runnable> shutdownNow() {
List<Runnable> tasks;
final ReentrantLock mainLock = this.mainLock;
mainLock.lock