I am working on a project which involves a lot of tasks of sending multiple requests at the same time and waiting to collect results. A general scheme can be described in the following code:
for(i=0; i<job_number; i++)
//send job i to remote machine to do long computation(an rpc call)
//wait for all remote job to finish, collect all results from rpc calls
//do aggregation based on the collected results
Based on my research, there are several possible solutions:
How is such problem being handled in practice? Which solution is better or more commonly used. Are there any other possible solutions? Thanks.
I would create a CompletableFuture
for each RPC and collect all of them in a list futures
:
for(i=0; i<job_number; i++)
futures.add(CompletableFuture.runAsync(/* Runnable for job i*/));
You can then reduce the Stream<CompletableFuture<T>>
futures.stream
to a CompletableFuture<Stream<T>>
as shown in this answer .
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.