Multithreading – waiting for any given future in scala?

Suppose I need to perform some parallel tasks in scala Each task performs some blocking calls (for example, process. Waitfor) Now I want to wait until any task is completed

As far as I know, I should use Scala future to wrap tasks Is there any API in scala waiting for any given futures to end?

Solution

There are built-in ways to do this:

Future.firstCompletedOf(yourFutures)

from the doc:

Please note that this does not interrupt all other futures, so you can cancel them yourself if necessary

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>