【发布时间】:2021-01-19 13:18:38
【问题描述】:
我正在尝试使用我自己的ExecutorService 创建一组CompletableFutures 来链接多个流程步骤。这些步骤可能会引发异常。
当他们这样做时,在我看来,ExecutorService 中的线程没有被释放,尽管我正在尝试处理这种情况。
class Scratch {
public static void main(String[] args) throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(10);
AtomicInteger counter = new AtomicInteger();
Supplier<?> throwingException = () -> { throw new RuntimeException("throw " + counter.incrementAndGet()); };
Function<String, CompletableFuture<?>> process =
url -> CompletableFuture.supplyAsync(throwingException, executor)
.exceptionally(Scratch::log);
var collect = IntStream.range(1, 10).mapToObj(i -> "url" + i)
.map(process)
.toArray(CompletableFuture[]::new);
final CompletableFuture<Void> together = CompletableFuture.allOf(collect);
System.out.println("joining");
together.exceptionally(Scratch::log).join();
System.out.println("finished");
if (executor.awaitTermination(5, TimeUnit.SECONDS)) {
System.out.println("exiting cleanly");
} else {
System.out.println("not terminated");
}
executor.submit(() -> System.out.println("still executing"));
}
static <T> T log(Throwable t) {
System.out.println(t.getMessage());
return null;
}
}
输出是
java.lang.RuntimeException: throw 1
joining
java.lang.RuntimeException: throw 2
java.lang.RuntimeException: throw 3
java.lang.RuntimeException: throw 4
java.lang.RuntimeException: throw 5
java.lang.RuntimeException: throw 6
java.lang.RuntimeException: throw 7
java.lang.RuntimeException: throw 8
java.lang.RuntimeException: throw 9
finished
not terminated
由此启动的进程也没有终止(这是我注意到的)。
在我看来,这应该意味着此时ExecutorService 中没有留下任何线程,但情况似乎并非如此;如果我们降低线程池容量,它仍然会运行所有提交的任务,如果我们在失败终止后添加另一个提交(例如executor.submit(() -> System.out.println("still executing"));),它将被执行。
如果我们不将自己的ExecutorService 传递给CompletableFutre::supplyAsync,进程将按预期终止。
我也尝试过处理异常状态的其他版本(例如使用together.whenComplete()),但结果相同。
为什么会发生这种情况,如何确保ExecutorService 正确终止?
编辑:我意识到这不是导致问题的异常,这将发生在使用您自己的执行程序服务提供给CompletableFuture 的任何任务中,鉴于 Eugene 的回复,这完全有道理。我正在更改问题标题。
【问题讨论】:
标签: java executorservice java.util.concurrent completable-future