简体   繁体   中英

Wait for any of given Futures in Scala?

Suppose I need to execute a few parallel tasks in Scala. Each task performs some blocking call (eg Process.waitFor ). Now I would like to wait until any of the tasks completes.

As I understand I should use Scala Future to wrap the task. Is there any API in Scala to wait until any of given Futures finishes?

There is build-in method for doing this:

Future.firstCompletedOf(yourFutures)

from the doc :

Returns a Future to the result of the first future in the list that is completed.

Note that this would not interrupt all other futures so you have cancel them by yourself, if you need to.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM