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 case of thenApplyAsync
:
future.thenApplyAsync( x -> x + 1 ) // first step
.thenApplyAsync( x -> x + 1 ) // second step
.thenAccept( x -> System.out.println(x)); // third step
I read in this blog that each thenApplyAsync
are executed in a separate thread and 'at the same time'(that means following thenApplyAsyncs
started before preceding thenApplyAsyncs
finish), if so, what is the input argument value of the second step if the first step not finished?
Where will the result of the first step go if not taken by the second step? the third step will take which step's result?
If the second step has to wait for the result of the first step then what is the point of Async
?
Here x -> x + 1 is just to show the point, what I want know is in cases of very long computation.
The difference has to do with the Executor
that is responsible for running the code. Each operator on CompletableFuture
generally has 3 versions.
thenApply(fn)
- runs fn
on a thread defined by the CompleteableFuture
on which it is called, so you generally cannot know where this will be executed. It might immediately execute if the result is already available.thenApplyAsync(fn)
- runs fn
on a environment-defined executor regardless of circumstances. For CompletableFuture
this will generally be ForkJoinPool.commonPool()
.thenApplyAsync(fn,exec)
- runs fn
on exec
.In the end the result is the same, but the scheduling behavior depends on the choice of method.