一个简单的线程池+CountDownLatch 的demo

实现功能
1.使用线程池执行一个任务List,
2.等待所有线程执行结果完成后返回结果
3.触发线程池创建新线程。
4.线程池关闭线程时非立即关闭验证。

 

//Thread 类

@Data
public class TaskDemo implements Runnable{

    //队列信息存储服务需要返回的结果
    private CountDownLatch countDownLatch;

    private Map outMap;
    @Override
    public void run() {
        try {
            Double randomNumber = (Math.random() * 10);
            int sleepSS = randomNumber.intValue();
            TimeUnit.SECONDS.sleep(sleepSS);
            System.out.println("线程执行:========="+Thread.currentThread().getName()+"===============");
            if (sleepSS%2==1) {
                outMap.put("resultCode","1");
                outMap.put("resultMsg",Thread.currentThread().getName()+"===1");
            }else {
                outMap.put("resultCode", "0");
                outMap.put("resultMsg",Thread.currentThread().getName()+"===0");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
        if (countDownLatch != null) {
            System.out.println("线程执行结束:========="+Thread.currentThread().getName()+"===============");
            countDownLatch.countDown();
        }
    }
    }
}

main函数

public static void main( String[] args ) throws InterruptedException {

        List<String> taskList= new ArrayList();
        for (int i = 0; i < 15; i++) {
            taskList.add(i+"");
        }
        Map innerMap = new HashMap();
        List<TaskDemo> TaskDemos = new ArrayList<TaskDemo>();
        LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue(5);
        CountDownLatch countDownLatch = new CountDownLatch(taskList.size());
        ExecutorService pool =
                new ThreadPoolExecutor(5, 10, 10L, TimeUnit.SECONDS, linkedBlockingQueue);

        for (String s : taskList) {
            Map outMap = new HashMap();
            TaskDemo runnable = new TaskDemo();
            runnable.setCountDownLatch(countDownLatch);
            runnable.setOutMap(outMap);
            pool.execute(runnable);
            TaskDemos.add(runnable);
        }


        countDownLatch.await();
        Long thisTime = System.currentTimeMillis();
        pool.shutdown();
        System.out.println(Thread.activeCount());
        System.out.println("End");
        while (Thread.activeCount()>2){
            System.out.println("Thread is 大于2");
        }
        System.out.println("Thread is 2");
        System.out.println(System.currentTimeMillis()-thisTime);
        for (TaskDemo demo : TaskDemos) {
            System.out.println(MapUtils.getString(demo.getOutMap(),"resultMsg"));
        }
//        TimeUnit.SECONDS.sleep(5);
//        System.out.println("End");
//        System.out.println(Thread.activeCount());
//        System.out.println("End");
    }

控制台打印

线程执行:=========pool-1-thread-4===============
线程执行结束:=========pool-1-thread-4===============
线程执行:=========pool-1-thread-7===============
线程执行结束:=========pool-1-thread-7===============
线程执行:=========pool-1-thread-1===============
线程执行:=========pool-1-thread-6===============
线程执行结束:=========pool-1-thread-6===============
线程执行结束:=========pool-1-thread-1===============
线程执行:=========pool-1-thread-10===============
线程执行结束:=========pool-1-thread-10===============
线程执行:=========pool-1-thread-7===============
线程执行结束:=========pool-1-thread-7===============
线程执行:=========pool-1-thread-3===============
线程执行结束:=========pool-1-thread-3===============
线程执行:=========pool-1-thread-8===============
线程执行:=========pool-1-thread-2===============
线程执行结束:=========pool-1-thread-2===============
线程执行:=========pool-1-thread-9===============
线程执行结束:=========pool-1-thread-9===============
线程执行:=========pool-1-thread-5===============
线程执行结束:=========pool-1-thread-5===============
线程执行结束:=========pool-1-thread-8===============
线程执行:=========pool-1-thread-4===============
线程执行结束:=========pool-1-thread-4===============
线程执行:=========pool-1-thread-1===============
线程执行结束:=========pool-1-thread-1===============
线程执行:=========pool-1-thread-6===============
线程执行结束:=========pool-1-thread-6===============
线程执行:=========pool-1-thread-10===============
线程执行结束:=========pool-1-thread-10===============
12
End
Thread is 大于2
Thread is 大于2
Thread is 大于2
Thread is 大于2
Thread is 大于2
Thread is 大于2
Thread is 2
1
pool-1-thread-1===0
pool-1-thread-2===0
pool-1-thread-3===1
pool-1-thread-4===0
pool-1-thread-5===0
pool-1-thread-4===1
pool-1-thread-7===1
pool-1-thread-6===1
pool-1-thread-1===1
pool-1-thread-10===0
pool-1-thread-6===0
pool-1-thread-7===1
pool-1-thread-8===0
pool-1-thread-9===0
pool-1-thread-10===1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值