Suppose I have the following code:
CompletableFuture<Integer> future
= CompletableFuture.supplyAsync( () -> 0);
thenApply case:
future.thenApply( x -> x + 1 )
.thenApply( x -> x + 1 )
.thenAccept( x -> System.out.println(x));
Here the output will be 2. Now in the case of thenApplyAsync:
future.thenApplyAsync( x -> x + 1 )
.thenApplyAsync( x -> x + 1 )
.thenAccept( x -> System.out.println(x));
I read in this blog that each thenApplyAsyncis executed in a separate thread and "at the same time" (this means the next thenApplyAsyncsstarted before the previous thenApplyAsyncsfinish), if so, what is the value of the input argument of the second step, if the first step is not completed?
Where will the result of the first step be if the second step is not taken? at what step will the third step be taken?
If the second step is to wait for the result of the first step, then what is the point Async?
Here x → x + 1 is just to show the point that I want to know in cases of very long calculations.