Andrew Scott Evans
Andrew Scott Evans

Reputation: 1033

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

Upvotes: 1

Views: 2516

Answers (2)

Zeimyth
Zeimyth

Reputation: 1395

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

Upvotes: 1

Peter Neyens
Peter Neyens

Reputation: 9820

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)

Upvotes: 7

Related Questions