- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
CompletionStage Javadoc 指出:
[...] if a stage's computation terminates abruptly with an (unchecked) exception or error, then all dependent stages requiring its completion complete exceptionally as well, with a CompletionException holding the exception as its cause.
看到异常完成总是在 CompletionException
中包装异常,为什么 exceptionally()
、whenComplete()
和 handle()
将异常表示为 Throwable
而不是 CompletionException
?
这很重要,因为它可以防止在这些方法中直接重新抛出异常。
这些方法是否可能接收到 CompletionException
以外的异常?或者我可以安全地强制转换为这种类型吗?
(我在本地运行了一些测试,并深入研究了 CompletableFuture 源代码,乍一看,我没有看到如何抛出任何其他类型的异常。)
最佳答案
Is it possible for these methods to receive an exception other than
CompletionException
?
是的,这是可能的,您不应该在没有instanceof
检查(或检查您的使用情况)的情况下强制转换为CompletionException
。
举个例子
CompletableFuture<Void> root = new CompletableFuture<>();
root.whenComplete((v, t) -> {
System.out.println(t.getClass()); // class java.io.IOException
});
root.completeExceptionally(new IOException("blow it up"));
whenComplete
将收到 IOException
而不是包装它的 CompletionException
。相同的行为适用于 exceptionally
和 handle
。
阶段的计算在 Javadoc 中定义:
The computation performed by a stage may be expressed as a
Function
,Consumer
, orRunnable
(using methods with names including apply, accept, or run, respectively) depending on whether it requires arguments and/or produces results.
我相信这句话
if a stage's computation terminates abruptly with an (unchecked) exception or error
指的是那些 Function#apply
、Consumer#accept
或 Runnable#run
方法之一,因为 thrown exception 突然终止,不是因为某个阶段通过其他机制异常完成。
另请注意,Javadoc 说
This interface does not define methods for initially creating, forcibly completing normally or exceptionally, probing completion status or results, or awaiting completion of a stage. Implementations of
CompletionStage
may provide means of achieving such effects, as appropriate
换句话说,该接口(interface)允许实现异常地完成阶段,而不会突然终止任何计算。我认为这允许新的行为。
如果我们扩展之前的例子
CompletableFuture<Void> root = new CompletableFuture<>();
CompletableFuture<Void> child = root.whenComplete((v, t) -> {
System.out.println(t.getClass()); // class java.io.Exception
});
child.whenComplete((v, t) -> {
System.out.println(t.getClass()); // class java.util.concurrent.CompletionException
});
root.completeExceptionally(new IOException("blow it up"));
您会注意到附加到 child
的完成收到一个 CompletionException
包装原始 IOException
。从 Javadoc 来看,这对我来说并不明显,它指出
Returns a new
CompletionStage
with the same result or exception as this stage
总而言之,似乎来自 completeExceptionally
的原始异常被传递给直接依赖项,而依赖项的依赖项接收封闭的 CompletionException
。
关于java - CompletionStage 是否总是将异常包装在 CompletionException 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49230980/
我们有一个内部API,它在lambda中执行代码,并在CompleteStage中返回布尔结果:。现在,第二步也被实现为一个cpdec任务:。如何让Second Step()在foo2()中运行,并确
假设我有这个方法: public CompletionStage getData() { CompletableFuture future = new CompletableFuture<>(
就像这个使用 of 的假设示例一样: List> listOfFutureLongs = getFutureLongs(...) CompletionStage> futureListOfLongs
我有两个类(class);A 和 B;都返回 CompletionStage。 B 类代码: class B { public CompletionStage>> m1(SampleObjec
场景: 有两个阶段 第二阶段仅在第一阶段完成后执行 第二阶段对第一阶段的结果不感兴趣,而仅仅对第一阶段完成的事实感兴趣 考虑现有方法: public CompletionStage thenAppl
我正在尝试使用 java 8 CompletionStages 串行执行 2 个异步方法,以便在第一个失败时不执行第二个。但是当我调用 thenCompose 时,传入的函数似乎在前一个函数完成之前就
我有两个 completionStages 方法调用,如果不满足条件,每个方法调用一个远程服务。它们都是长时间运行的进程,我们需要减少延迟。我也不关心 secondFuture 的响应。它可以返回 C
在 exceptionally 内重新抛出异常似乎不允许使用 CompletionStage 方法。 我需要检查某种异常,如果没有,我需要重新抛出它: Future futureSite = some
CompletionStage Javadoc 指出: [...] if a stage's computation terminates abruptly with an (unchecked) e
这是我的方法: public CompletionStage insert(List hashActionList) { if(!hashActionList.isEmpty()) {
当调用从 Netty 处理程序内部返回 CompletionStage 的服务时,如何最好地处理异常。 我认为有两种类型的异常需要处理: 在被调用服务内部生成 CompletionStage 时发生的
所以考虑到我有以下示例: CompletionStage tokenFuture = getToken(); CompletionStage>>> result = tokenFuture.thenA
我试图在以下代码中找到更好的方法来处理多个异常: public CompletionStage getRepositoryInfo(String repositoryOwner, String rep
我正在用 Java 编写一个 Play2 应用程序服务方法,它应该执行以下操作。异步调用A方法,失败再异步调用B方法。 为了说明,假设此接口(interface)用于服务调用的后端: public i
CompletionStage Javadoc 指出: [...] if a stage's computation terminates abruptly with an (unchecked) e
我在它们每个中都看到了一个示例,但我需要确切地知道 deep 中的区别是什么,因为有时我认为我可以同时使用它们来获得相同的结果,所以我想知道以便我可以选择正确的? 使用它们各自有什么好处? 就像这个例
如果有 2 个 CompletionStages,我可以将它们与 thenCombine 方法结合起来: CompletionStage aCompletionStage = getA(); Comp
这个问题已经有答案了: CompletableFuture recoverWith equivalent? i.e. exceptionally but return CompletableFutur
在构建 API 时,对接口(interface)进行编码是一种很好的做法,因此返回 CompletionStage 似乎是一种最佳方法。但是我意识到,在获得 CompletionStage 之后,我碰
我遇到了一个问题,当方法返回 CompletionStage 时,我的过滤器运行了两次。从关于 RequestMapping ( here ) 的文档中,它是受支持的返回值。 A Completion
我是一名优秀的程序员,十分优秀!