[英]Why Java 8 CompletableFuture thenCompose generates different exception depending on the order of completion?
[英]Completion order of CompletableFuture
当 CompletableFuture 有多个依赖项时,我很想了解完成顺序。 我本来希望家属按照他们添加的顺序完成,但情况似乎并非如此。
特别是,这种行为令人惊讶:
import java.util.concurrent.CompletableFuture;
public class Main {
public static void main(String[] args) {
{
var x = new CompletableFuture<Void>();
x.thenRun(() -> System.out.println(1));
x.thenRun(() -> System.out.println(2));
x.thenRun(() -> System.out.println(3));
x.complete(null);
}
{
var x = new CompletableFuture<Void>();
var y = x.copy();
y.thenRun(() -> System.out.println(1));
y.thenRun(() -> System.out.println(2));
y.thenRun(() -> System.out.println(3));
x.complete(null);
}
}
}
...导致以下 output ...
3
2
1
1
2
3
'x.thenRun()' 表达式生成的所有 3 个子期货并行运行,因此您不能期望任何特定的打印数字顺序。
'y.thenRun()' 也是如此。
表达的顺序是一种实现特性,将来可能会改变,并且不受规范的支持。
所以你没有链接那些thenRun
,因此对订单有零保证,但你期望一些订单? 对不起,这不是它的工作方式。
文档没有说明这种情况下的执行顺序,所以无论您现在在 output 中看到什么:
使用当前设置通过代码证明有点困难,但只需将其更改为:
var x = CompletableFuture.runAsync(() -> {
LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1));
});
x.thenRun(() -> System.out.println(1));
x.thenRun(() -> System.out.println(2));
x.thenRun(() -> System.out.println(3));
x.join();
运行这个假设20 times
,我打赌至少一次,你不会看到3, 2, 1
,而是不同的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.