guys! I have a question: what does this code do:
Collection<Contract.class> contracts = fillTheCollectionFromDb();
contracts.stream().filter(condition)
.map(contractItem ->
CompletableFuture.supplyAsync(() ->
{T result = getAnotherDataFromDb(contractItem);
return result;}, Executor.class)
)//end .map
.map(CompletableFuture::join).collect(Collectors.toList());
this code is equivalent to:
Collection<Contract> contracts = fillTheCollectionFromDb();
contracts.stream().filter(condition)
.map(this::getAnotherDataFromDb)
.collect(Collectors.toList());
To make this program truly parallel, it should be modified. First, launch all the requests to database in parallel:
Collection<Contract> contracts = fillTheCollectionFromDb();
List<CompletableFuture> futures = contracts.stream().filter(condition)
.map(contractItem ->
CompletableFuture.supplyAsync(
()->getAnotherDataFromDb(contractItem),
executor)
)//end .map
.collect(Collectors.toList());
and only after that collect all the results:
List results = futures.stream
.map(CompletableFuture::join)
.collect(Collectors.toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With