Timeout while waiting for a batch of Futures to complete?

纵然是瞬间 提交于 2020-12-30 06:50:27

问题


I have a set of Futures created by submitting Callables 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 Futures 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 Futures 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!