Michael
Michael

Reputation: 42110

Futures for blocking calls in Scala

The Akka documentation says:

you may be tempted to just wrap the blocking call inside a Future and work with that instead, but this strategy is too simple: you are quite likely to find bottlenecks or run out of memory or threads when the application runs under increased load.

They suggest the following strategies:

Do you know about any implementation of those strategies?

Upvotes: 9

Views: 2463

Answers (2)

Marius Danila
Marius Danila

Reputation: 10431

Futures are run within execution contexts. This is obvious from the Future API: any call which involves attaching some callbacks to a future or to build a future from an arbitrary computation or from another future requires an implicitly available ExecutionContext object. So you can control the concurrency setup for your futures by tuning the ExecutionContext in which they run.

For instance, to implement the second strategy you can do something like

import scala.concurrent.ExecutionContext
import java.util.concurrent.Executors
import scala.concurrent.future

object Main extends App {

  val ThreadCount = 10
  implicit val executionContext = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(ThreadCount))

  val f = future {
    println(s"Hello ! I'm running in an execution context with $ThreadCount threads")
  }

}

Upvotes: 17

themel
themel

Reputation: 8895

Akka itself implements all this, you can wrap your blocking calls into Actors and then use dispatchers to control execution thread pools.

Upvotes: 2

Related Questions