Suppose
CompletableFuture<T> cf = CompletableFuture.completedFuture(...);
cf.thenApplyAsync(f)
.thenApplyAsync(g);
cf.thenApplyAsync(h);
By chanining the calls to f and g we get a guaranteed ordering. What about h ? Do we get any guarentees that h will allways be executed after g ?
I tried putting g to sleep in order to test this and I always see h executing after g but this is not evidence. If it is the case that h is always executed after g where can I find some documentation about this ? I read the CompletionStage, ExecutorService and CompletableFuture docs but I haven't really found any info that would lead me to a conclusion on ordering when chaining like in the case of (g . f) is not involved.
If it's not in javadocs, it's not guaranteed. In your case, h
can easily be executed before g
. All it would take for this to happen is for f
to take a while:
cf.thenApplyAsync(i -> {
try {
System.out.println("F");
Thread.sleep(5000);
return i;
} catch(InterruptedException e) {
throw new RuntimeException(e);
}
}).thenApplyAsync(i -> {
System.out.println("G");
return i;
});
cf.thenApplyAsync(i -> {
System.out.println("H");
return i;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.