title: How to get the ThreadPoolExecutor to increase threads to max before queueing?
body: <p>I've been frustrated for some time with the default behavior of <code>ThreadPoolExecutor</code> which backs the <code>ExecutorService</code> thread-pools that so many of us use.  To quote from the Javadocs:</p>
  <blockquote>
  <p>If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created <strong>only if the queue is full</strong>.</p>
  </blockquote>
  <p>What this means is that if you define a thread pool with the following code, it will <em>never</em> start the 2nd thread because the <code>LinkedBlockingQueue</code> is unbounded.</p>
  <pre><code>ExecutorService threadPool =
     new ThreadPoolExecutor(1 /*core*/, 50 /*max*/, 60 /*timeout*/,
        TimeUnit.SECONDS, new LinkedBlockingQueue&lt;Runnable&gt;(/* unlimited queue*/));
  </code></pre>
  <p>Only if you have a <em>bounded</em> queue and the queue is <em>full</em> are any threads above the core number started.  I suspect a large number of junior Java multithreaded programmers are unaware of this behavior of the <code>ThreadPoolExecutor</code>.</p>
  <p>Now I have specific use case where this is not-optimal.  I'm looking for ways, without writing my own TPE class, to work around it.</p>
  <p>My requirements are for a web service that is making call-backs to a possibly unreliable 3rd party.</p>
  <ul>
  <li>I don't want to make the call-back synchronously with the web-request, so I want to use a thread-pool.</li>
  <li>I typically get a couple of these a minute so I don't want to have a <code>newFixedThreadPool(...)</code> with a large number of threads that mostly are dormant.</li>
  <li>Every so often I get a burst of this traffic and I want to scale up the number of threads to some max value (let's say 50).</li>
  <li>I need to make a <em>best</em> attempt to do all callbacks so I want to queue up any additional ones above 50.  I don't want to overwhelm the rest of my web-server by using a <code>newCachedThreadPool()</code>.</li>
  </ul>
  <p>How can I work around this limitation in <code>ThreadPoolExecutor</code> where the queue needs to be bounded and full <em>before</em> more threads will be started?  How can I get it to start more threads <em>before</em> queuing tasks?</p>
  <p><strong>Edit:</strong></p>
  <p>@Flavio makes a good point about using the <code>ThreadPoolExecutor.allowCoreThreadTimeOut(true)</code> to have the core threads timeout and exit.  I considered that but I still wanted the core-threads feature.  I did not want the number of threads in the pool to drop below the core-size if possible.</p>