使用线程池后是否会异步执行的笔记

1.直接在方法里使用线程池,能实现异步,会继续执行主线程代码

public void threadTest(){
        ThreadPoolExecutor executor = ThreadUtil.buildThreadPool(5, "tttt");
        for (int i = 0; i < 5; i++){
            executor.execute(new RunnableTest(i));
        }
        logger.info("都跑好了");
    }

    private class RunnableTest implements Runnable{
        int i;

        public RunnableTest(int i){
            this.i = i;
        }

        @Override
        public void run() {
            ThreadUtil.threadWait(10000);
            logger.info(i + "跑好了");
        }
    }

上述代码执行结果为:

2021-05-24 11:38:33,678 [http-nio-16100-exec-5] INFO  c.y.s.b.c.controller.TestController [TestController.java : 28] - 都跑好了
2021-05-24 11:38:43,679 [tttt-4] INFO  c.y.s.b.c.controller.TestController [TestController.java : 41] - 3跑好了
2021-05-24 11:38:43,680 [tttt-1] INFO  c.y.s.b.c.controller.TestController [TestController.java : 41] - 0跑好了
2021-05-24 11:38:43,679 [tttt-5] INFO  c.y.s.b.c.controller.TestController [TestController.java : 41] - 4跑好了
2021-05-24 11:38:43,680 [tttt-2] INFO  c.y.s.b.c.controller.TestController [TestController.java : 41] - 1跑好了
2021-05-24 11:38:43,679 [tttt-3] INFO  c.y.s.b.c.controller.TestController [TestController.java : 41] - 2跑好了

2.当需要等待多线程跑完,再继续执行主代码,则可以使用CountDownLatch

public void threadTest(){
        ThreadPoolExecutor executor = ThreadUtil.buildThreadPool(5, "tttt");
        CountDownLatch countDownLatch = new CountDownLatch(5);
        for (int i = 0; i < 5; i++){
            executor.execute(new RunnableTest(i, countDownLatch));
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            logger.info("出现异常", e);
        }
        logger.info("都跑好了");
    }

    private class RunnableTest implements Runnable{
        int i;
        CountDownLatch countDownLatch;

        public RunnableTest(int i, CountDownLatch countDownLatch){
            this.i = i;
            this.countDownLatch = countDownLatch;
        }

        @Override
        public void run() {
            try {
                ThreadUtil.threadWait(10000);
                logger.info(i + "跑好了");
            } finally {
                countDownLatch.countDown();
            }
        }
    }

上述代码执行结果为:

2021-05-24 11:44:11,563 [tttt-2] INFO  c.y.s.b.c.controller.TestController [TestController.java : 51] - 1跑好了
2021-05-24 11:44:11,563 [tttt-5] INFO  c.y.s.b.c.controller.TestController [TestController.java : 51] - 4跑好了
2021-05-24 11:44:11,563 [tttt-4] INFO  c.y.s.b.c.controller.TestController [TestController.java : 51] - 3跑好了
2021-05-24 11:44:11,564 [tttt-3] INFO  c.y.s.b.c.controller.TestController [TestController.java : 51] - 2跑好了
2021-05-24 11:44:11,564 [tttt-1] INFO  c.y.s.b.c.controller.TestController [TestController.java : 51] - 0跑好了
2021-05-24 11:44:11,564 [http-nio-16100-exec-7] INFO  c.y.s.b.c.controller.TestController [TestController.java : 35] - 都跑好了

3.但需要返回值时,可以用Callable,但在主线程中用Future接收,会导致后续的主线程代码阻塞

public void threadTest() throws ExecutionException, InterruptedException {
        ThreadPoolExecutor executor = ThreadUtil.buildThreadPool(5, "tttt");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 5; i++){
            Future<String> future = executor.submit(new CallableTest(i));
            sb.append(future.get()).append(";");
        }
        logger.info(sb.toString());
        logger.info("都跑好了");
    }

    private class CallableTest implements Callable<String> {
        int i;

        public CallableTest(int i){
            this.i = i;
        }


        @Override
        public String call() throws Exception {
            String s = i + "跑好了";
            logger.info(s);
            return s;
        }
    }

上述代码的执行结果为:

2021-05-24 11:55:31,299 [tttt-1] INFO  c.y.s.b.c.controller.TestController [TestController.java : 45] - 0跑好了
2021-05-24 11:55:31,301 [tttt-2] INFO  c.y.s.b.c.controller.TestController [TestController.java : 45] - 1跑好了
2021-05-24 11:55:31,302 [tttt-3] INFO  c.y.s.b.c.controller.TestController [TestController.java : 45] - 2跑好了
2021-05-24 11:55:31,303 [tttt-4] INFO  c.y.s.b.c.controller.TestController [TestController.java : 45] - 3跑好了
2021-05-24 11:55:31,304 [tttt-5] INFO  c.y.s.b.c.controller.TestController [TestController.java : 45] - 4跑好了
2021-05-24 11:55:31,305 [http-nio-16100-exec-6] INFO  c.y.s.b.c.controller.TestController [TestController.java : 30] - 0跑好了;1跑好了;2跑好了;3跑好了;4跑好了;
2021-05-24 11:55:31,306 [http-nio-16100-exec-6] INFO  c.y.s.b.c.controller.TestController [TestController.java : 31] - 都跑好了

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值