问题
I have a set of Futures
created by submitting Callable
s to an Executor
. Pseudo code:
for all tasks
futures.add(executor.submit(new callable(task)))
Now I'd like to get all futures waiting at most n seconds until all complete. I know I can call Future#get(timeout)
but if I call that sequentially for all my futures in a loop the timouts start adding up. Pseudo code:
for all futures
future.get(timeout)
get
blocks with a timeout until the result is ready. Therefore, if the first completes just before timeout and the second also completes just before timeout and so on the entire execution time is number of futures * timeout
at most instead of timeout
.
Hence, I'm looking for a method that accepts a list of Future
s and a timeout, runs all in parallel and then returns a collection of future results. Any ideas?
回答1:
You can use ExecutorService.invokeAll:
Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first.
Future.isDone()
is true for each element of the returned list. Upon return, tasks that have not completed are cancelled. Note that a completed task could have terminated either normally or by throwing an exception. The results of this method are undefined if the given collection is modified while this operation is in progress.
If you already have Future
s that you need to monitor and can't use invokeAll
, you can simply measure the timeout yourself. Pseudo code:
long endTime = System.currentTimeMillis() + timeoutMS;
for(f : futures)
f.get(Math.max(0, endTime - System.currentTimeMillis()), TimeUnit.MILLISECONDS);
This way you give each future at most the duration that is left until you reach your timeout.
来源:https://stackoverflow.com/questions/17434311/timeout-while-waiting-for-a-batch-of-futures-to-complete