1 void shutdownAndAwaitTermination(ExecutorService pool) {
2 pool.shutdown(); // Disable new tasks from being submitted
3 try {
4 // Wait a while for existing tasks to terminate
5 if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
6 pool.shutdownNow(); // Cancel currently executing tasks
7 // Wait a while for tasks to respond to being cancelled
8 if (!pool.awaitTermination(60, TimeUnit.SECONDS))
9 System.err.println("Pool did not terminate");
10 }
11 } catch (InterruptedException ie) {
12 // (Re-)Cancel if current thread also interrupted
13 pool.shutdownNow();
14 // Preserve interrupt status
15 Thread.currentThread().interrupt();
16 }
转自:https://stackoverflow.com/questions/36376179/executorservices-shutdown-doesnt-wait-until-all-threads-will-be-finished