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 }
JAVA & Android 等待线程池内任务全部完成后退出
猜你喜欢
转载自www.cnblogs.com/zl1991/p/9089360.html
今日推荐
周排行