我当前正在使用CompletableFuture supplyAsync()方法向公共线程池提交一些任务。这是代码段的样子:
final List<CompletableFuture<List<Test>>> completableFutures = resolvers.stream()
.map(resolver -> supplyAsync(() -> task.doWork()))
.collect(toList());
CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()])).join();
final List<Test> tests = new ArrayList<>();
completableFutures.stream()
.map(completableFuture -> completableFuture.getNow())
.forEach(tests::addAll);
我想知道下面与上面的代码有何不同。我从下面的代码中删除了父completableFuture,并为每个completableFuture添加了join()而不是getNow():
final List<CompletableFuture<List<Test>>> completableFutures = resolvers.stream()
.map(resolver -> supplyAsync(() -> task.doWork()))
.collect(toList());
final List<Test> tests = new ArrayList<>();
completableFutures.stream()
.map(completableFuture -> completableFuture.join())
.forEach(tests::addAll);
我在spring服务中使用它,并且线程池耗尽存在问题。任何指针深表感谢。
答案 0 :(得分:3)
首先,.getNow()
不起作用,因为对于将来尚未完成的情况,此方法需要使用后备值作为参数。由于您假设将来会在这里完成,因此您也应该使用join()
。
然后,线程耗尽没有任何区别,因为在两种情况下,您都在等待所有作业的完成之后再继续操作,可能会阻塞当前线程。
避免这种情况的唯一方法是,重构代码以使其不期望同步结果,而是安排在完成所有作业后执行后续处理动作。然后,使用allOf
就变得很重要:
final List<CompletableFuture<List<Test>>> completableFutures = resolvers.stream()
.map(resolver -> supplyAsync(() -> task.doWork()))
.collect(toList());
CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture<?>[0]))
.thenAccept(justVoid -> {
// here, all jobs have been completed
final List<Test> tests = completableFutures.stream()
.flatMap(completableFuture -> completableFuture.join().stream())
.collect(toList());
// process the result here
});
顺便说一下,关于集合的toArray
方法,我建议阅读Arrays of Wisdom of the Ancients…