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

package com.testSome;  
  
import java.util.ArrayList;  
import java.util.List;  
import java.util.concurrent.ExecutorService;  
import java.util.concurrent.Executors;  
/** 
 *  
 * @author Sailing 
 * 
 */  
public class TestThreadPool{  
    class myThread implements Runnable{  
        private Integer type;  
        public myThread() {  
            super();  
        }  
        public myThread(Integer type) {  
            super();  
            this.type = type;  
        }  
        @Override  
        public void run() {  
            System.out.println("type:"+type);  
        }  
    }  
      
    public void doUpdate(){  
        ExecutorService es = Executors.newCachedThreadPool();  
        System.out.println("开始");  
        List<Integer> typeList = new ArrayList<>();  
        typeList.add(1);  
        typeList.add(2);  
        typeList.add(3);  
        typeList.add(4);  
        typeList.add(5);  
        typeList.add(6);  
        typeList.add(7);  
        typeList.add(8);  
        typeList.add(9);  
        typeList.add(10);  
        for(int i=0;i<typeList.size();i++){  
            Integer adjustType = typeList.get(i);  
            Runnable myRunnable = new myThread(adjustType);  
            es.execute(myRunnable);  
        }  
        /* 
         * es.shutdown();       阻止新任务的提交,但是原本已经提交的,不会受到影响,当已提交的任务全部完成后,中断闲置的线程 
         * es.shutdownNow();    阻止新任务的提交,且已提交的任务也会受到影响,不等已提交的任务完成,就会中断所有的线程。 
         */  
        es.shutdown();  
        /* 
         * es.isTerminated();   调用shutdown()方法后,且已提交的任务完成后,才会返回true 
         * es.isShutdown();     调用shutdown()方法后,立马返回true 
         */  
        if(es.isTerminated()){  
            System.out.println("结束");     
        }  
    }  
    public static void main(String[] args) {  
        new TestThreadPool().doUpdate();  
    }  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值