title: How to tune akka thread pool to reject new tasks on full thread pool and task queue?
body: <p>I want to use akka thread pool to perform some parallel tasks with limited resources and if there are too many tasks I want them to be rejected.</p>
  
  <p>I have the following dispatcher configured with akka</p>
  
  <pre><code>my-dispatcher {
    type = Dispatcher
    executor = thread-pool-executor
    thread-pool-executor {
      core-pool-size-min = 1
      core-pool-size-max = 2
      task-queue-size = 5
    }
  }
  </code></pre>
  
  <p>And the following code</p>
  
  <pre><code>import java.time.LocalTime
  
  import akka.actor.ActorSystem
  
  import scala.concurrent.duration.Duration
  import scala.concurrent.{Await, Future}
  
  object Main {
  
    def main(args: Array[String]) {
      val system = ActorSystem("my-system")
      implicit val dispatcher = system.dispatchers.lookup("my-dispatcher")
  
      for (_ &lt;- 1 to 10) Future {
        Thread.sleep(5000)
        println(s"Finished at ${ LocalTime.now }")
      }
  
      Await.result(system.terminate(), Duration.Inf)
    }
  
  }
  </code></pre>
  
  <p>I expected that after the thread pool has running out free threads (maximum size is 2) and task-queue is also full (5 elements) - it will reject new Futures. However, that's what I see:</p>
  
  <pre><code>Finished at 17:36:57.858
  Finished at 17:36:57.858
  Finished at 17:36:57.858
  Finished at 17:36:57.858
  Finished at 17:36:57.858
  Finished at 17:37:02.870
  Finished at 17:37:02.870
  Finished at 17:37:02.870
  Finished at 17:37:02.870
  Finished at 17:37:02.870
  </code></pre>
  
  <p>Thus, it invokes first 5 tasks, finishes them almost at the same time (17:36:57), and after a pause of 5 seconds (<code>Thread.sleep(5000)</code>) at 17:37:02 finishes the second portion of tasks. It means that when queue is full and thread pool is completely busy - instead of rejecting new futures the <code>Future.apply(???)</code> invocation just blocks. I want to see some sort of RejectionException or similar.</p>
  
  <p>How to achieve this behaviour with akka?</p>