【发布时间】:2019-11-26 13:57:54
【问题描述】:
如何一次将两个任务添加到 Executor(或类似的),然后等待两个中的任何一个完成(即最快),而另一个以及之前启动的任务继续在后台?
我知道 CompletionService 提供了类似的东西,但我能做的就是等待下一个完成,.take()。在我的情况下,这可能来自以前的计划,而不是我需要等待的计划之一。
我想要的伪代码
ExecutorService executorService = Executors.newFixedThreadPool(100);
Future<?> one = executorService.submit(() -> oneWay());
Future<?> two = executorService.submit(() -> orAnother());
Future theFirstOneToFinish = waitFor(one, two);
// one done, the other one keeps on working
return theFirstOneToFinish;
【问题讨论】:
标签: java concurrency parallel-processing