Skip to content

Instantly share code, notes, and snippets.

@larroy
Created April 3, 2016 10:27
Show Gist options
  • Save larroy/86c228d765b52a33ad1d737ae94a51cf to your computer and use it in GitHub Desktop.
Save larroy/86c228d765b52a33ad1d737ae94a51cf to your computer and use it in GitHub Desktop.
import java.util.concurrent.{TimeUnit, ArrayBlockingQueue, ThreadPoolExecutor}
import scala.concurrent.ExecutionContext
object ThrottlingExecutionContext {
def apply(poolSize: Int, maxQueued: Int): ExecutionContext = {
val workQueue = new ArrayBlockingQueue[Runnable](maxQueued) {
override def offer(x: Runnable): Boolean = {
put(x)
true
}
}
ExecutionContext.fromExecutorService(
new ThreadPoolExecutor(poolSize, poolSize, 0L, TimeUnit.SECONDS, workQueue)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment