gpt4 book ai didi

multithreading - 等待斯卡拉的任何指定 future 吗?

转载 作者:行者123 更新时间:2023-12-04 13:33:05 25 4
gpt4 key购买 nike

假设我需要在Scala中执行一些并行任务。每个任务都会执行一些阻止调用(例如Process.waitFor)。现在,我想等到所有任务完成。

据我了解,我应该使用Scala Future来包装任务。 Scala中是否有任何API可以等到给定的Futures中的任何一个完成?

最佳答案

有内置的方法可以做到这一点:

Future.firstCompletedOf(yourFutures)

from the doc:

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



请注意,这不会中断所有其他 future ,因此您可以根据需要自行取消它们。

关于multithreading - 等待斯卡拉的任何指定 future 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15950430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com