判斷線程池所有任務是否執行完畢
阿新 • • 發佈:2017-11-13
exe current cat pack adp cut value clas catch
package com.yd.wmsc.util; import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class Test { public static void main(String[] args) throws IOException, InterruptedException { ExecutorService service= Executors.newFixedThreadPool(2); for (int i = 0; i < 4; i++) { Runnable run = new Runnable() { @Override public void run() { try { Thread.currentThread().sleep(10000); System.out.println("1"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }; } }; service.execute(run); } service.shutdown(); service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); System.out.println("all thread complete"); } }
判斷線程池所有任務是否執行完畢