我是靠谱客的博主 沉默小天鹅,这篇文章主要介绍java8 乱序_为什么Java 8 CompletableFuture thenCompose会根据完成顺序生成不同的异常?...,现在分享给大家,希望可以做个参考。

我遇到过

Java 8 CompletableFuture thenCompose方法的奇怪行为.我有两个测试,仅在执行顺序上有所不同.两个测试都模拟thenCompose中生成的CompletableFuture中的失败.

@Test

public void completedAfter() {

CompletableFuture future1 = new CompletableFuture<>();

CompletableFuture future2 = new CompletableFuture<>();

future1.thenCompose(x -> future2).whenComplete((r, e) -> System.out.println("After: " + e));

future1.complete("value");

future2.completeExceptionally(new RuntimeException());

}

@Test

public void completedBefore() {

CompletableFuture future1 = new CompletableFuture<>();

CompletableFuture future2 = new CompletableFuture<>();

future1.complete("value");

future2.completeExceptionally(new RuntimeException());

future1.thenCompose(x -> future2).whenComplete((r, e) -> System.out.println("Before: " +e));

}

输出是:

After: java.util.concurrent.CompletionException: java.lang.RuntimeException

Before: java.lang.RuntimeException

问题是,为什么在一个案例中包含在CompletionException中但在另一个案例中却没有包含异常?

更新:Here是相关的错误报告.它已被标记并解析为JDK中的错误.

最后

以上就是沉默小天鹅最近收集整理的关于java8 乱序_为什么Java 8 CompletableFuture thenCompose会根据完成顺序生成不同的异常?...的全部内容,更多相关java8内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部