title: ThreadPoolExecutor and the queue
body: <p>I thought that using <a href="http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html" rel="noreferrer">ThreadPoolExecutor</a> we can submit <code>Runnable</code>s to be executed either in the <code>BlockingQueue</code> passed in the constructor or using the <code>execute</code> method.<br>
  Also my understanding was that if a task is available it will be executed.<br>
  What I don't understand is the following:  </p>
  
  <pre><code>public class MyThreadPoolExecutor {  
  
      private static ThreadPoolExecutor executor;  
  
      public MyThreadPoolExecutor(int min, int max, int idleTime, BlockingQueue&lt;Runnable&gt; queue){  
          executor = new ThreadPoolExecutor(min, max, 10, TimeUnit.MINUTES, queue);   
          //executor.prestartAllCoreThreads();  
      }  
  
      public static void main(String[] main){
          BlockingQueue&lt;Runnable&gt; q = new LinkedBlockingQueue&lt;Runnable&gt;();
          final String[] names = {"A","B","C","D","E","F"};  
          for(int i = 0; i &lt; names.length; i++){  
              final int j = i;  
              q.add(new Runnable() {  
  
                  @Override  
                  public void run() {  
                      System.out.println("Hi "+ names[j]);  
  
                  }  
              });         
          }  
          new MyThreadPoolExecutor(10, 20, 1, q);   
          try {  
              TimeUnit.SECONDS.sleep(5);  
          } catch (InterruptedException e) {  
              // TODO Auto-generated catch block  
              e.printStackTrace();  
          }  
          /*executor.execute(new Runnable() {  
  
              @Override  
              public void run() {  
  
                  System.out.println("++++++++++++++");  
  
              }   
          });  */
          for(int i = 0; i &lt; 100; i++){  
              final int j = i;  
              q.add(new Runnable() {   
  
                  @Override  
                  public void run() {  
                      System.out.println("Hi "+ j);  
  
                  }  
              });  
          }   
  
  
      }  
  
  
  }
  </code></pre>
  
  <p>This code does not do absolutely anything unless I either uncomment the <code>executor.prestartAllCoreThreads();</code> in the constructor OR I call <code>execute</code> of the runnable that prints <code>System.out.println("++++++++++++++");</code> (it is also commented out).  </p>
  
  <p>Why?<br>
  Quote (my emphasis):  </p>
  
  <blockquote>
    <p>By default, even core threads are initially created and <strong>started only
    when new tasks arrive</strong>, but this can be overridden dynamically using
    method prestartCoreThread() or prestartAllCoreThreads(). You probably
    want to prestart threads if you construct the pool with a non-empty
    queue.  </p>
  </blockquote>
  
  <p>Ok. So my queue is not empty. But I create the <code>executor</code>, I do <code>sleep</code> and then I add new <code>Runnable</code>s to the queue (in the loop to 100).<br>
  Doesn't this loop count as <code>new tasks arrive</code>?<br>
  Why doesn't it work and I have to either <code>prestart</code> or explicitely call <code>execute</code>?</p>