ExecutorService的典型关闭方法
2020-10-22 本文已影响0人
大风过岗
关闭ExecutorService的方法
private shutdownExecutorService(){
//1、停止线程池接收新任务
//2、等待一定时间,让现存的任务执行结束
//3、取消当前运行的任务
//4、如果当前线程也被中断的话,那么就再次关闭线程池,同时恢复中断状态
pool.shutdown();
try{
if(!pool.awaitTermination(60,TimeUnit.SECONDS)){
pool.shutdownNow();
if(!pool.awaitTermination(60,TimeUnit.SECONDS)){
System.out.println("Pool did not terminate");
}
}
}catch(InterruptedException ie){
pool.shutdownNow();
Thread.currentTread().interrupt():
}
}