简体   繁体   中英

Scala Execute List of Futures Concurrently

I am new to scala. It is helping to reduce code and provide the elements of a functional language to deal with data. However, I am having trouble finding a way to execute a lst of futures in parallel. My list is of the type List[Future[String]]. How can I get this list to execute in parallel?

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

The Future is defined as:

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)
  }

You can use Future.traverse .

Quote from the Scala documentation :

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)

The Futures will begin executing as soon as they have an available thread to run them. Assuming you have an execution context with multiple threads, that means they will automatically execute in parallel.

You can demonstrate this with the 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

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