'How to efficiently process List<Future> without blocking
I am trying to figure out if there is a way to process a List items in a non blocking way ?
The scenario is as follows
- There is method gets invoked to send a Collection of requests to remote API
- Each request contains an ID
- For each request that is sent the remote API returns a Future<SomeData>
- For all futures complete successfully the method collects the associated request ID and sends it back.
Is it possible to check if the all the collected futures are now done without blocking ? Not sure if there is a way around future.get() while not losing the information to which request the future is associated with. The remote api does provide an option specify a call back. But I am unsure how to leverage that in way that won't be blocking in some way.
Future<SomeData> processRequests(Data d, CallBack());
Solution 1:[1]
If you simply want to test if all of the futures have completed, failed or been cancelled:
public boolean allDone(List<Future<?>> futures) {
for (f Future<?>: futures) {
if (!f.isDone()) {
return false;
}
}
return true;
}
Obviously, that (potentially) entails checking each one. But that is unavoidable.
On the other hand, if you need to wait until the futures are all done, that will (potentially) block. That also is unavoidable.
On the other hand, if you need to process the futures that have completed without blocking on those that haven't, do something like this:
public <T> void processDone(List<Future<T>> futures, Consumer<T> process) {
for (Iterator<Future<T>> it = futures.iterator();
it.hasNext; /* */) {
Future<T> f = it.next();
if (f.isDone()) {
process(f.get());
it.remove();
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
