Skip to content

Instantly share code, notes, and snippets.

@satendrakumar
Last active September 20, 2017 21:15
Show Gist options
  • Save satendrakumar/a83f1107745aa57630e5a9756a2ec745 to your computer and use it in GitHub Desktop.
Save satendrakumar/a83f1107745aa57630e5a9756a2ec745 to your computer and use it in GitHub Desktop.
Choosing an ExecutorService for application(use own thread pool instead of Scala default )
import java.util.concurrent.{Executors, ForkJoinPool}
import scala.concurrent.ExecutionContext
//For IO intensive applications
object MyExecutionContext {
private val concurrency = Runtime.getRuntime.availableProcessors()
implicit val commonThreadPool: ExecutionContext = ExecutionContext.fromExecutor(Executors.newCachedThreadPool())
}
//heavy/blocking io intensive applications
object IOExecutionContext {
private val concurrency = Runtime.getRuntime.availableProcessors()
implicit val ioThreadPool: ExecutionContext = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(concurrency * 10))
}
//for cpu intensive applications
object FJExecutionContext {
private val concurrency = Runtime.getRuntime.availableProcessors()
implicit val forkJoinThreadPool: ExecutionContext = ExecutionContext.fromExecutor(new ForkJoinPool(concurrency))
}
//Example :use own thread pool instead of scala default
import MyExecutionContext.commonThreadPool
val futureResult = Future{2 + 4 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment