ExecutorCompletionService? Why do need one if we have invokeAll?

后端 未结 4 1746
渐次进展
渐次进展 2020-12-04 09:48

If we use an ExecutorCompletionService we can submit a series of tasks as Callables and get the result interacting with the CompletionService as a

4条回答
  •  旧巷少年郎
    2020-12-04 10:18

    By using an ExecutorCompletionService, you can get immediately notified when each of your jobs completes. In comparison, ExecutorService.invokeAll(...) waits for all of your jobs to complete before returning the collection of Futures:

    // this waits until _all_ of the jobs complete
    List> futures = threadPool.invokeAll(...);
    

    Instead, when you use a ExecutorCompletionService, you will be able to get the jobs as soon as each of them completes which allows you to (for example) send them on for processing into another thread pool, log results, etc..

    ExecutorService threadPool = Executors.newFixedThreadPool(2);
    ExecutorCompletionService compService
          = new ExecutorCompletionService(threadPool);
    for (MyJob job : jobs) {
        compService.submit(job);
    }
    // shutdown the pool but the jobs submitted continue to run
    threadPool.shutdown();
    while (!threadPool.isTerminated()) {
        // the take() blocks until any of the jobs complete
        // this joins with the jobs in the order they _finish_
        Future future = compService.take();
        // this get() won't block
        Result result = future.get();
        // you can then put the result in some other thread pool or something
        // to immediately start processing it
        someOtherThreadPool.submit(new SomeNewJob(result));
    }
    

提交回复
热议问题