ExecutorCompletionService 的使用

Future 有一个问题就是,他的get 方法是把全部数据处理完再拿出来的。

ExecutorCompletionService 可以做到拿一条输出一条

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

        ExecutorService executorService = Executors.newFixedThreadPool(2);

        final List<Callable<Integer>> callableList = Arrays.asList(
                () -> {
                    sleep(10);
                    System.out.println("sleep 10 seconds");
                    return 10;
                },
                () -> {
                    sleep(20);
                    System.out.println("sleep 10 seconds");
                    return 20;
                }
        );
        ExecutorCompletionService<Integer> completionService = new ExecutorCompletionService(executorService);
        List<Future<Integer>> futures = new ArrayList<>();
        callableList.stream().forEach(callable -> futures.add(completionService.submit(callable)));

        Future<Integer> future;
        //take 会阻塞
        // poll 方法不是阻塞方法,拿的时候会出现空指针
        while((future = completionService.take()) != null){
            System.out.println(future.get());
        }

    }

 public static void sleep(int time){
        try {
            TimeUnit.SECONDS.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

场景应用

在实际开发的时候,会在一定时间内处理一些问题,但是没处理的问题,也需要保留起来
ExecutorCompletionService 关注的是已经完成的东西

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

        // ExecutorCompletionService 关注得是已完成得
        // 如果shutdownNow 方法调用,我是没办法知道
        /*ExecutorService service = Executors.newSingleThreadExecutor();
        List<Runnable> task = IntStream.range(0, 5).boxed().map(ScheduleExecutorServiceTest::toTask).collect(Collectors.toList());
        ExecutorCompletionService<Object> completionService = new ExecutorCompletionService<>(service);
        task.forEach(r -> completionService.submit(Executors.callable(r)));

        TimeUnit.SECONDS.sleep(13);
        List<Runnable> runnables = service.shutdownNow();
        System.out.println(runnables);*/

        // 以下方式自定义能拿到所有未完成得
        ExecutorService service = Executors.newSingleThreadExecutor();
        List<Callable> task = IntStream.range(0, 5).boxed().map(MyTask::new).collect(Collectors.toList());
        ExecutorCompletionService<Integer> completionService = new ExecutorCompletionService<>(service);
        task.forEach(completionService::submit);

        TimeUnit.SECONDS.sleep(13);
        service.shutdownNow();
//        System.out.println(runnables);
        task.stream().filter(callable -> !((MyTask)callable).isSuccess()).forEach(System.out::println);

    }

    private static class MyTask implements Callable<Integer>{

        private final Integer value;

        private boolean success = false;

        private MyTask(Integer value) {
            this.value = value;
        }

        @Override
        public Integer call() throws Exception {
            System.out.println("the task will be executed" + value);
            TimeUnit.SECONDS.sleep(value * 5 + 5);
            System.out.println("the task executed done" + value);
            success = true;
            return value;
        }

        public boolean isSuccess() {
            return success;
        }
    }

    private static Runnable toTask(int i){

        return () -> {
            try {
                System.out.println("the task will be executed" + i);
                TimeUnit.SECONDS.sleep(i * 5 + 5);
                System.out.println("the task executed done" + i);
            } catch (InterruptedException e) {
                System.out.println("the task be interrupted" + i);            }
        };
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值