如果返回失败的阶段,为什么 thenCombine 结果不会异常完成?
Why thenCombine result doesn't complete exceptionally if returning a failed stage?
以下代码片段调用 thenCombine, does not set an exception at whenComplete(它打印 No exception
):
CompletableFuture.completedFuture(true)
.thenCombine(CompletableFuture.completedFuture(true),
(x,y) -> {
return CompletableFuture.failedStage(new RuntimeException());
})
.whenComplete( (myVal, myEx) -> {
if (myEx == null) {
System.out.println("No exception");
} else {
System.out.println("There was an exception");
}
});
但是,下面调用 thenCompose 的类似代码确实设置了异常:
CompletableFuture.completedFuture(true)
.thenCompose(
x -> {
return CompletableFuture.failedStage(new RuntimeException());
})
.whenComplete( (myVal, myEx) -> {
if (myEx == null) {
System.out.println("No exception");
} else {
System.out.println("There was an exception");
}
});
为什么 thenCombine
返回一个正常完成的 CompletionStage
而它的 BiFunction 实际上返回一个失败的阶段?
在您的第一个示例中 CompletableFuture.failedStage(new RuntimeException())
是 结果。 CompletableFuture
return 由 thenCombine
完成 CompletableFuture.failedStage(new RuntimeException())
。
当您不链接调用时,它会变得更清晰:
CompletableFuture<Boolean> first = CompletableFuture.completedFuture(true);
CompletableFuture<Boolean> second = CompletableFuture.completedFuture(true);
CompletableFuture<CompletionStage<?>> yourQuestion =
first.thenCombine(second, (x, y) ->
CompletableFuture.failedStage(new RuntimeException()));
对于您的第二个示例,您需要仔细阅读 thenCompose
的文档:
Returns a new CompletionStage that is completed with the same value as the CompletionStage returned by the given function.
在 thenCompose
的函数中,您 return 一个失败的 CompletableFuture
并且 它的结果 被用作 [=12] 的结果=] return编辑者 thenCompose
:RuntimeException
.
以下代码片段调用 thenCombine, does not set an exception at whenComplete(它打印 No exception
):
CompletableFuture.completedFuture(true)
.thenCombine(CompletableFuture.completedFuture(true),
(x,y) -> {
return CompletableFuture.failedStage(new RuntimeException());
})
.whenComplete( (myVal, myEx) -> {
if (myEx == null) {
System.out.println("No exception");
} else {
System.out.println("There was an exception");
}
});
但是,下面调用 thenCompose 的类似代码确实设置了异常:
CompletableFuture.completedFuture(true)
.thenCompose(
x -> {
return CompletableFuture.failedStage(new RuntimeException());
})
.whenComplete( (myVal, myEx) -> {
if (myEx == null) {
System.out.println("No exception");
} else {
System.out.println("There was an exception");
}
});
为什么 thenCombine
返回一个正常完成的 CompletionStage
而它的 BiFunction 实际上返回一个失败的阶段?
在您的第一个示例中 CompletableFuture.failedStage(new RuntimeException())
是 结果。 CompletableFuture
return 由 thenCombine
完成 CompletableFuture.failedStage(new RuntimeException())
。
当您不链接调用时,它会变得更清晰:
CompletableFuture<Boolean> first = CompletableFuture.completedFuture(true);
CompletableFuture<Boolean> second = CompletableFuture.completedFuture(true);
CompletableFuture<CompletionStage<?>> yourQuestion =
first.thenCombine(second, (x, y) ->
CompletableFuture.failedStage(new RuntimeException()));
对于您的第二个示例,您需要仔细阅读 thenCompose
的文档:
Returns a new CompletionStage that is completed with the same value as the CompletionStage returned by the given function.
在 thenCompose
的函数中,您 return 一个失败的 CompletableFuture
并且 它的结果 被用作 [=12] 的结果=] return编辑者 thenCompose
:RuntimeException
.