zoukankan      html  css  js  c++  java
  • CompletionService的应用场景

    如下场景:我们在一个线程池中提交了多个任务,多个任务的执行时长不一样,而且我们想按执行时间的长短(先拿到执行时间短的任务的结果,再拿执行时间长的任务的结果)来依次获取任务的返回结果。

    那可以应用CompletionService来方便的处理这个场景。

    示例代码如下:

    package completionServiceDemo;
    
    import java.util.Date;
    import java.util.concurrent.*;
    
    public class Demo {
    
        public static void main(String[] args) throws InterruptedException, ExecutionException {
            ExecutorService executorService = Executors.newFixedThreadPool(5);
            CompletionService<Integer> completionService = new ExecutorCompletionService<>(executorService);
            completionService.submit(new MyCallable(10));
            completionService.submit(new MyCallable(20));
            completionService.submit(new MyCallable(5));
            for (int i = 0; i < 3; i++) {
                Integer integer = completionService.take().get();
                System.out.println(new Date() + ":" + integer);
            }
        }
    }
    package completionServiceDemo;
    
    import java.util.concurrent.Callable;
    
    public class MyCallable implements Callable<Integer> {
    
        Integer seconds;
    
        public MyCallable(int seconds) {
            this.seconds = seconds;
        }
    
        @Override
        public Integer call() throws Exception {
            Thread.sleep(seconds * 1000);
            return seconds;
        }
    }

    打印结果如下:

    Thu Jul 29 23:26:55 CST 2021:5
    Thu Jul 29 23:27:00 CST 2021:10
    Thu Jul 29 23:27:10 CST 2021:20

    原理:

    CompletionService将各任务执行的结果放到一个BlockingQueue中(先执行完的先往里放),再从队列往外取的时候(队列先进先出)就自然达到了按执行快慢获取结果的效果。

  • 相关阅读:
    [NOI2005]维修数列
    [USACO07OPEN]吃饭Dining
    [TJOI2010]打扫房间
    [SCOI2005]最大子矩阵
    [HNOI2007]最小矩形覆盖
    [HAOI2006]受欢迎的牛
    BZOJ2087[Poi2010] Sheep
    [USACO08DEC]在农场万圣节Trick or Treat on the Farm
    [POI2013]BAJ-Bytecomputer
    HGOI20190126 模拟赛
  • 原文地址:https://www.cnblogs.com/silenceshining/p/15077638.html
Copyright © 2011-2022 走看看