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?
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.
在您的示例中,此处的用法异步不会对您造成任何影响,因为您正在用此调用join,因此join阻止了呼叫,并且Async将不再是ASync
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.