title: Removing all queued tasks of an ThreadPoolExecutor
body: <p>i have this rather simple question about the <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html" rel="noreferrer">ThreadPoolExecutor</a>. I have the following situation: I have to consume objects from a queue, create the appropiate worker tasks for them and submit them to the ThreadPoolExecutor. This is quite simple. But within a shutdown scenario <strong>many</strong> workers may be queued to execution. Since one of those tasks might be running for an hour, and i want a relativly fast graceful shutdown of the application i want to discard all queued tasks from the ThreadPoolExecutor while the already processing tasks should be completed normally.</p>
  
  <p>The ThreadPoolExecutor documentation has a <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html#remove(java.lang.Runnable)" rel="noreferrer">remove()</a> method but only allows specific tasks to be removed. <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html#purge()" rel="noreferrer">purge()</a> only works for already canceled Future tasks. My idea was to clear the  queue holding all queued tasks. The ThreadPoolExecutor provides access to this internal queue but the documentation states:</p>
  
  <blockquote>
    <p>Method getQueue() allows access to the
    work queue for purposes of monitoring
    and     debugging. Use of this method
    for any other purpose is strongly
    discouraged.</p>
  </blockquote>
  
  <p>So grabbing this queue and clearing it is not an option. Also, this snippet of the documentation says:</p>
  
  <blockquote>
    <p>Two supplied methods,
    remove(java.lang.Runnable) and purge()
    are available to assist in storage
    reclamation when large numbers of
    queued tasks become cancelled.</p>
  </blockquote>
  
  <p>How? Sure, i can maintain a list of all tasks i submitted to the executor and in a shutdown case i iterate over all entries and remove them from the ThreadPoolExecutor with the remove() method... but... come on, this is a waste of memory and a hassle to maintain this list. (Removing already executed tasks for example)</p>
  
  <p>I appreciate any hints or solutions!</p>