Creating a thread pool in Scala

Part of the magic of Scala is that you can easily parallelize operations with features like Futures and parallel collections. When you do this, scala creates a thread pool, so that it can allocate appropriate resources.

If you want to control this, you can make your own thread pool by copying the following code. This is passed to the futures API as an implicit, so it will “just work” as long as you paste it before the parallelism code (any easy way to prove this is to make the thread pool very large, and watch your machine grind to a halt)

implicit val ec = new ExecutionContext {
  val threadPool = Executors.newFixedThreadPool(25)

  def execute(runnable: Runnable) {
    threadPool.submit(runnable)
  }

  def reportFailure(t: Throwable) {}
}

You can then give tasks to the pool with futures:

val tasks =
  for (i <- 1 to 1000000) yield Future {
    doSomething
  }
 
val aggr = Future.sequence(tasks)
Await.result(aggr, Duration.Inf)

For the above code to work, you'll need the following imports:

import scala.actors.threadpool.Executors
import scala.concurrent.duration.Duration