Folks I have been using CompletableFuture in my project and I experienced a strange behaviour. I would like to understand the behaviour. Kindly help
Scenario 1: In the below code the output is I am from supply I am the first thenApply I am the second thenApply .. As expected
public void callAll(){
String calculatedDistance = listBuildComp().stream()
.map(CompletableFuture::join)
.collect(Collectors.joining());
System.out.println(calculatedDistance);
}
private List<CompletableFuture<String>> listBuildComp(){
List<CompletableFuture<String>> result = new ArrayList<>();
result.add(buildComp());
return result;
}
private CompletableFuture<String> buildComp(){
CompletableFuture<String> workFlowWithServices =
CompletableFuture.supplyAsync( () -> "I am from supply ")
.thenApply( x -> {
return x.concat(" I am the first thenApply ");
})
.thenApply( x -> {
return x.concat(" I am the second thenApply ");
});
return workFlowWithServices;
}
Scenario 2: When the below method is changed then the output is I am from supply . Upon further investigation I see that the rest two thenApply runs in their own thread
private CompletableFuture<String> buildComp(){
CompletableFuture<String> workFlowWithServices =
CompletableFuture.supplyAsync( () -> "I am from supply ");
workFlowWithServices.thenApply( x -> {
return x.concat(" I am the first thenApply ");
});
workFlowWithServices.thenApply( x -> {
return x.concat(" I am the second thenApply ");
});
return workFlowWithServices;
}
The reason I am interested with Scenario 2 is imagine you are chaining 2 TASKS then Scenario 1 is okay but imagine you want to chain 50 TASKS then the method will get too big. In this case I wanted to extract each calls in to a method to begin with but eventually extract in to a class if required but I cannot do all these cause of Scenario 2.
Want to know the concept or idea about why scenario 2 behaves in a different way and if there is anyway to make it behave like scenario 1. Kindly share your knowledge. Thank you.