Scala并发执行期货列表

Scala Execute List of Futures Concurrently

我是 Scala 新手。它有助于减少代码并提供功能语言的元素来处理数据。但是,我无法找到一种方法来并行执行一系列期货。我的列表是 List[Future[String]] 类型。我怎样才能让这个列表并行执行?

val futures=(data.map { x => this.breakString(x) }).toList

未来定义为:

def breakString(inX:Object):Future[String]=Future {
    //get new jsonObject
    val x =inX.asInstanceOf[String]
    val jmap=JacksMapper.readValue[Map[String,AnyRef]](x)
    val dataArr:Array[String]=jmap.get(this.rowcolumn).asInstanceOf[String].split(token)
    val map=dataArr.map { x => (positions.get(dataArr.indexOf(x).asInstanceOf[String]),x) }.toMap
    map.put(hashKey, jmap.get(hashKey).asInstanceOf[String])

    //write out positions
    JacksMapper.writeValueAsString(map)
  }

您可以使用 Future.traverse.

引用自 Scala 文档:

This is useful for performing a parallel map. For example, to apply a function to all items of a list in parallel

Future.traverse(data)(breakString)

Futures 将在有可用线程时立即开始执行 运行。假设您有一个包含多个线程的执行上下文,这意味着它们将自动并行执行。

您可以使用 Scala REPL 进行演示:

scala> import scala.concurrent.Future
import scala.concurrent.Future

scala> import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.ExecutionContext.Implicits.global

scala> def makeFuture(ms: Int, msg: String): Future[Unit] = Future { Thread.sleep(ms); println(msg) }
makeFuture: (ms: Int, msg: String)scala.concurrent.Future[Unit]

scala> makeFuture(1000, "a"); makeFuture(500, "b")
res2: scala.concurrent.Future[Unit] = scala.concurrent.impl.Promise$DefaultPromise@2819f703

scala> b
a