Last active
September 20, 2017 21:15
-
-
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 )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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