gpt4 book ai didi

asynchronous - CompletableFuture.exceptionally 与 executor

转载 作者:行者123 更新时间:2023-12-04 04:24:29 27 4
gpt4 key购买 nike

CompletableFuture.exceptionally() method采用 lambda,但没有采用自定义 Executor 的方法的风格,甚至没有它的“...异步”风格。

lambda 在哪个执行器上运行?它会是运行抛出异常的原始 CompletableFuture 的同一个执行程序吗?或者(如果是这种情况,我会感到惊讶)它是 commonPool 吗?

最佳答案

表单 JDK 错误讨论 CompletableFuture.exceptionally may execute on main thread :

CompletableFuture.exceptionally does not take an Executor argument since it is not designed to execute the exceptionally task asynchronously.

If the dependent task is not yet completed then the exceptionally task will complete on the same thread that dependent tasks completes on.

If the dependent task is completed then the exceptionally task will complete on the thread that executed the call to exceptionally.

This is the same behaviour that will occur for any non-asynchronous execution, such as thenAccept.

To guarantee that the exceptionally task is executed on a thread within the executor thread pool then it is necessary to use whenCompleteAsync or handleAsync and passing in the executor.

关于asynchronous - CompletableFuture.exceptionally 与 executor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38254268/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com