title: How does newCachedThreadPool reuse threads?
body: <p>I want to ask for a little more detail to the same question posted by Zeller over a year ago...</p>
  
  <p><i>The javadoc says that the service returned by <code>Executors.newCachedThreadPool</code> reuses threads. How is this possible?</i></p>
  
  <p>I get how the queue structure is setup internally, what I don't see is how it reuses threads in the queue.</p>
  
  <p>All examples I've seen have the developer create an instance of their thread and pass it in through the "execute" method.</p>
  
  <p>For example...</p>
  
  <pre><code>ExecutorService executor = Executors.newCachedThreadPool();
  for (int i = 0; i &lt; 10; i++) {            
    Runnable worker = new WorkerThread(i);  //will create 10 instances          
    executor.execute(worker);          
  } 
  </code></pre>
  
  <p>I understand that a thread pool can easily manage the life cycle of each thread, but again, I see no methods nor the ability to access or restart any of the threads in the pool.</p>
  
  <p>In the above example, I would then expect that each thread would be started, run, terminated and disposed of by the thread pool, but never reused.</p>
  
  <p>A messaging system would be an example of where you'd need this.  Say you have an onMessage handler and you'd like to reuse one of the threads in the pool to handle it, so I'd expect methods like...</p>
  
  <pre><code>worker = executor.getIdleThread;
  worker.setData(message);
  executor.resubmit(worker);          
  </code></pre>
  
  <p>or maybe have the ExecutorService acting as a factory class and have it return an instance of your threads, where internally it decides to create a new one or reuse an old one.</p>
  
  <pre><code>ExecutorService executor = Executors.newCachedThreadPool(WorkerThread);
  Runnable worker = executor.getThread;
  worker.setData(message);
  </code></pre>
  
  <p>So I'm missing something.  It's probably something simple but I've spent the afternoon reading tutorials and examples and still haven't figured it out.  Can someone shed some light on the subject?</p>