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

来源:互联网 发布:淘宝卖家收款的钱在哪 编辑:程序博客网 时间:2024/05/16 15:17

newFixedThreadPool

创建一个固定大小的线程池。

shutdown():用于关闭启动线程,如果不调用该语句,jvm不会关闭。

awaitTermination():用于等待子线程结束,再继续执行下面的代码。该例中我设置一直等着子线程结束。

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() {                       System.out.println("thread start");                   }               };               service.execute(run);           }           service.shutdown();           service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);           System.out.println("all thread complete");       }   } 


0 0
原创粉丝点击