ExecutorCompletionService和ExecutorService的区别

转载记录-原文

public class ExecutorServiceTest {


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        testFuture();
        System.out.println("****************************************************");
       testCompletionService();
    }

    //结果的输出和线程的放入顺序 有关(如果前面的没完成,就算后面的哪个完成了也得等到你的牌号才能输出!),so阻塞耗时
    public static void testFuture() throws InterruptedException, ExecutionException {
        System.out.println("ExecutorService--1.1--> main Thread begin:");
        ExecutorService executor = Executors.newCachedThreadPool();
        List<Future<String>> result = new ArrayList<Future<String>>();
        for (int i = 0; i < 10; i++) {
            Future<String> submit = executor.submit(new Task(i));
            result.add(submit);
        }
        executor.shutdown();
        for (int i = 0; i < 10; i++) {
            //一个一个等待返回结果
            System.out.println("--1.2--> order result: " + result.get(i).get());
        }
        System.out.println("--1.3--> main Thread end:");
    }

    //结果的输出和线程的放入顺序 无关(谁完成了谁就先输出!主线程总是能够拿到最先完成的任务的返回值,而不管它们加入线程池的顺序),
    // so很大大缩短等待时间
    private static void testCompletionService() throws InterruptedException, ExecutionException {
        System.out.println("ExecutorCompletionService--2.2--> main Thread begin:");
        ExecutorService executor = Executors.newCachedThreadPool();
        ExecutorCompletionService<String> completionService = new ExecutorCompletionService<>(executor);
        for (int i = 0; i < 10; i++) {
            completionService.submit(new Task(i));
        }
        executor.shutdown();
        for (int i = 0; i < 10; i++) {
            // 检索并移除表示下一个已完成任务的 Future,如果目前不存在这样的任务,则等待。
            //这一行没有完成的任务就阻塞
            Future<String> future = completionService.take();
            // 这一行在这里不会阻塞,引入放入队列中的都是已经完成的任务
            System.out.println("--2.3--> " + future.get());
        }
        System.out.println("--2.4--> main Thread end:");
    }

    private static class Task implements Callable<String> {

        private volatile int i;

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

        @Override
        public String call() throws Exception {
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName());
            return "--0.1--> TASK : " + i;
        }

    }
}

返回结果如下

ExecutorService--1.1--> main Thread begin:
pool-1-thread-8
pool-1-thread-6
pool-1-thread-3
pool-1-thread-2
pool-1-thread-1
pool-1-thread-7
pool-1-thread-5
pool-1-thread-10
pool-1-thread-4
pool-1-thread-9
--1.2--> order result: --0.1--> TASK : 0
--1.2--> order result: --0.1--> TASK : 1
--1.2--> order result: --0.1--> TASK : 2
--1.2--> order result: --0.1--> TASK : 3
--1.2--> order result: --0.1--> TASK : 4
--1.2--> order result: --0.1--> TASK : 5
--1.2--> order result: --0.1--> TASK : 6
--1.2--> order result: --0.1--> TASK : 7
--1.2--> order result: --0.1--> TASK : 8
--1.2--> order result: --0.1--> TASK : 9
--1.3--> main Thread end:
****************************************************
ExecutorCompletionService--2.2--> main Thread begin:
pool-2-thread-7
pool-2-thread-10
pool-2-thread-2
pool-2-thread-3
pool-2-thread-9
pool-2-thread-8
pool-2-thread-4
pool-2-thread-1
pool-2-thread-5
pool-2-thread-6
--2.3--> --0.1--> TASK : 6
--2.3--> --0.1--> TASK : 9
--2.3--> --0.1--> TASK : 1
--2.3--> --0.1--> TASK : 2
--2.3--> --0.1--> TASK : 8
--2.3--> --0.1--> TASK : 7
--2.3--> --0.1--> TASK : 3
--2.3--> --0.1--> TASK : 0
--2.3--> --0.1--> TASK : 4
--2.3--> --0.1--> TASK : 5
--2.4--> main Thread end:

ExecutorService:结果的输出和线程的放入顺序 有关

ExecutorCompletionService:拿到最先完成的任务的返回值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值