看看ExecutorCompletionService。它可以满足您的所有需求。
void solve(Executor e, Collection<Callable<Result>> solvers) throws InterruptedException, ExecutionException { //This class will hold and execute your tasks CompletionService<Result> ecs= new ExecutorCompletionService<Result>(e); //Submit (start) all the tasks asynchronously for (Callable<Result> s : solvers)ecs.submit(s); //Retrieve completed task results and use them int n = solvers.size(); for (int i = 0; i < n; ++i) {Result r = ecs.take().get();if (r != null) use(r); } }使用CompletionService的好处是它总是返回第一个完成的结果。这样可以确保您不等待任务完成,并且可以使未完成的任务在后台运行。



