问题描述
我遇到了一个奇怪的情况。我正在摆弄 CompletableFuture
,在运行以下代码时,我得到了意想不到的结果: public static void main(String [] args){
CompletableFuture< CompletableFuture< CompletableFuture< CompletableFuture< CompletableFuture< CompletableFuture< Object>>>>>> completableFutureCompletableFuture = CompletableFuture.supplyAsync(() - > {
System.out.println(first);
返回CompletableFuture.supplyAsync(() - > {
System.out。 println(second);
返回CompletableFuture.supplyAsync(() - > {
System.out.println(third);
返回CompletableFuture.supplyAsync(() - > ; {
System.out.println(4th);
返回CompletableFuture.supplyAsync(() - > {
System.out.println(Fifth);
返回CompletableFuture.completedFuture(null);
});
});
});
});
});
completableFutureCompletableFuture.get();
}
不抛出任何异常(即使特别使用
)我看到的是控制台输出是
第一个
秒
第三个//有时出现
现在,显然这段代码没有真正的生产价值,但这是一种表示如果您的代码具有未知数量的嵌套,其中每个嵌套或其中一些嵌套创建 CompleteableFutures
,这将不会被执行。
任何解释(以及如何修复的例子)将不胜感激
之所以如此不起作用是因为在您的简单测试中,VM在所有任务完成之前退出。
当您致电 completableFutureCompletableFuture.get()
只保证期货的第一个嵌套完成。 VM退出,所有线程都被杀死。
换句话说,第一个嵌套的未来可能仍然是未完成,因为它的线程可能仍然很忙。但是,当您尝试使用 get
获得结果时,它当然会等到它完成并且它将按预期工作。试试:
completableFutureCompletableFuture.get()。get()。get()。get()。get()
...然后你强制所有期货已经完成,一切都按预期工作。
I encountered a strange situation. I'm fiddling with CompletableFuture
and when running the following code I have unexpected results:
public static void main(String[] args) {
CompletableFuture<CompletableFuture<CompletableFuture<CompletableFuture<CompletableFuture<CompletableFuture<Object>>>>>> completableFutureCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("first");
return CompletableFuture.supplyAsync(() -> {
System.out.println("second");
return CompletableFuture.supplyAsync(() -> {
System.out.println("third");
return CompletableFuture.supplyAsync(() -> {
System.out.println("fourth");
return CompletableFuture.supplyAsync(() -> {
System.out.println("fifth");
return CompletableFuture.completedFuture(null);
});
});
});
});
});
completableFutureCompletableFuture.get();
}
No exception is thrown (even when using exceptionally
) and what I see is that the console output is
first
second
third // appears sometimes
Now, obviously this code has no real production value but this is a representation of a case where your code has an unknown number of nestings where each, or some of them, create CompleteableFutures
which won't be executed.
Any explanation (and example on how to fix) would be greatly appreciated
The reason why this doesn't work is because in your simple test the VM exits before all tasks are completed.
When you call completableFutureCompletableFuture.get()
only the first nesting of the futures is guaranteed to have finished. The VM exits, and all threads get killed.
In other words, the first nested future could still be "uncompleted" as its thread might still be busy. However, when you try to get its result with get
it will of course wait until it completed and it will work as expected. Just try:
completableFutureCompletableFuture.get().get().get().get().get()
... then you force all futures to have completed and everything works as expected.
这篇关于嵌套期货未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!