I'm curious to understand the completion order when a CompletableFuture has multiple dependents. I would have expected the dependents to complete in the order they were added, but that doesn't seem to be the case.
In particular, this behaviour is surprising:
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);
}
}
}
...results in the the following output...
3
2
1
1
2
3