判断线程池所有任务是否执行完毕

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");   
    }   
} 

 

posted @ 2017-11-13 19:04  tonggc1668  阅读(356)  评论(0编辑  收藏  举报