3
votes

I want to define 2 thread-pools in my application. One fork-join and one thread-pool executors. Furthermore each pool should be able to be shared by Akka actors, Scala futures and Scala parallel collections.

For future, scala needs execution context in scope and can be created in one of the following ways:

implicit val ec = ExecutionContext.global //want to avoid this
import scala.concurrent.ExecutionContext.Implicits.global //want to avoid this
implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(64))
implicit val ec = ExecutionContext.fromExecutor(Executors.newForkJoinThreadPool(64))

For, parallel collection you have to modify TaskSupport like this

val forkJoinPool = new java.util.concurrent.ForkJoinPool(64)
parArray.tasksupport = new ForkJoinTaskSupport(forkJoinPool)

With above, my only option is to define val forkJoinPool = new java.util.concurrent.ForkJoinPool(64) globally in my application and use it for both.

However, I don't know how to utileze same pool for Akka actors. For Akka, I see at least following 2 ways to customize pool.

val actorSysterm = ActorSystem.create("hello-system", config.getConfig("my-dispatcher”)) 
implicit val executionContext = actorSysterm.dispatcher

implicit val system = ActorSystem()
implicit val executionContext = actorSysterm.dispatchers.lookup("my-dispatcher")

And this is based on configuration file.

my-dispatcher {
  type = Dispatcher
  executor = "fork-join-executor"
  fork-join-executor {
    parallelism-min = 8
    parallelism-factor = 2.0
    parallelism-max = 64
  }
  throughput = 100
}
blocking-io-dispatcher {
  type = Dispatcher
  executor = "thread-pool-executor"
  thread-pool-executor {
    fixed-pool-size = 32
  }
  throughput = 1
}

I prefer Akka to create ExecutionContext because now I can configure my pools in hocon(json) file and can use it in play framework app as well. I can use it with Scala Futures but how to use it with Scala Parallel collection now? Is there a way to access underlaying pool from ExecutionContext so I can use it to initialize TaskSupport of parallel collection?

1

1 Answers

1
votes

There's an another implementation of TaskSupport used by parallel collection which I overlooked earlier. With that I can use same executionContext that I am using for Future and ActorSystem.

pc.tasksupport = new ExecutionContextTaskSupport(executionContext)