Reputation: 9
Having:
String getData(String key){
// Slow operation
}
CompletableFuture<String> getDataAsync(String key){
return CompletableFuture.supplyAsync(() -> getData(key));
}
What is the difference between the following?
a)
keyStream.map(key -> getData(key));
b)
keyStream.map(key -> getDataAsync(key).join());
Is there any advantage to use async in this case?
Upvotes: 0
Views: 1717
Reputation: 858
Usage Async here in your example will not make difference as you are calling join with that so join is blocking call and Async will no more be ASync
Upvotes: 0
Reputation: 30686
IF the stream in both approach a and b are sequentially. there is no different between them, and you should avoiding use it. because you just run the getData
in the ForkJoinPool.commonPool()
& join the the map
method until getData
was completed.
IF the stream in both approach a and b are parallelism. there is no different between them too, and you also should avoiding use it. because a parallel stream is already using the ForkJoinPool.commonPool()
for its operations.
Upvotes: 1