title: Interrupting BlockingQueue take() in a thread pool
body: <p>I have a thread pool which creates workers and the workers take the jobs from a <code>BlockingQueue</code>.
  The threads wait on <code>take()</code> from the queue.
  Even on explicitly calling the thread interrupt method for the running threads, they are still waiting on <code>take()</code>. What is right way of dealing with <code>blockingqueue</code></p>
  
  <pre><code>public class ThreadPoolGen {
      static final Logger LOG = Logger.getLogger(ThreadPoolGen.class);
  
      private LinkedBlockingQueue&lt;Runnable&gt; queue;
      private int threadCount;
      private Worker[] workers;
      private Thread[] workerThreads;
  
      public ThreadPoolGen(int count) throws CountException{
  
          if(isValidCount(count))
              this.threadCount = count;
          else
              throw new CountException("Invalid Thread Count");
  
          workers = new Worker[count];
          workerThreads = new Thread[count];
          queue = new LinkedBlockingQueue&lt;Runnable&gt;();
          startThreads();
      }
  
      public boolean execute(Runnable task){
          return queue.offer(task);
      }
  
      private void startThreads(){
          synchronized (this) {
  
              for(int i=0;i&lt;threadCount;i++){
  
                  workers[i] = new Worker();
                  workerThreads[i] = new Thread(workers[i]);
                  workerThreads[i].start();
              }
          }
      }
  
      public boolean shutDown(){
          try{
              for(Worker w: workers){
  
                  w.thread.interrupt();
  
              }
  
              queue.clear();
  
              for(Thread workerThread : workerThreads){
  
                  workerThread.interrupt();
  
              }
  
              return true;
  
          }catch(Exception e){
              LOG.debug(Thread.currentThread()+": Worker Thread Shutdown Failed");
              return false;
  
          }
  
      }
  
      private boolean isValidCount(int count){
  
          if(count&lt;Integer.MAX_VALUE &amp;&amp; count&gt;0)
              return true;
          else
              return false;
      }
  
      private class Worker implements Runnable{
  
          final Thread thread;
  
          private Worker(){
              this.thread = Thread.currentThread();
          }
  
          @Override
          public void run() {
              try{
                  while(true){
                      try{
                          Runnable r = queue.take();
                          r.run();
                      }catch(InterruptedException interrupt){
                          LOG.debug("Interrupted exception in: "+thread.getName());
                      }
                  }
              }catch(Exception intr){
  
                  this.thread.interrupt();
  
              }finally{
  
                  this.thread.interrupt();
              }
          }
      }
  }
  </code></pre>
  
  <p>The calling class :</p>
  
  <pre><code>public class Runner {
      public static void main(String[] args) {
          try {
              System.out.println("BeforeLaunch");
              ThreadPoolGen gen = new ThreadPoolGen(10);
              gen.execute(new Runnable() {
  
                  @Override
                  public void run() {
                      System.out.println("Inside Runnable");
  
                  }
              });
  
              gen.shutDown();
          } catch (CountException ce) {
          } catch (Exception e) {
          }
  
      }
  
  }
  </code></pre>