废话少说:
1.ExecutorService.shutdown与ExecutorService.shutdownNow的区别?
ExecutorService.shutdown 停止接收新的task提交,完成已提交的task
ExecutorService.shutdownNow 停止接收新的task,并停止线程池中已提交的task
正确的关闭ExecutorService流程
public static void shutdownAndAwaitTermination(ExecutorService es) {
es.shutdown(); // Disable new tasks from being submitted 不再接受新的提交
try {
// Wait a while for existing tasks to terminate
if (!es.awaitTermination(BaseConstants.AWAIT_TIME, TimeUnit.MILLISECONDS)) {
es.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!es.awaitTermination(BaseConstants.AWAIT_TIME, TimeUnit.MILLISECONDS))
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
es.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
有新的补