title: Thread Interrupt while sleeping
body: <p>In the example below I am attempting to gracefully terminate both threads. The consumer thread gets interrupted while sleeping, which should set the isInterrupted flag to true. However the !Thread.currentThread().isInterrupted() check on the while loop still seems to return = false because it does not terminate the consumer thread.</p>
  
  <p>Copy and paste the code below into and IDE to check:</p>
  
  <pre><code>public class ThreadInterruptExample {
  
  public static void main(String[] args) throws InterruptedException {
      LinkedBlockingQueue&lt;String&gt; queue = new LinkedBlockingQueue&lt;String&gt;(1);
      ThreadInterruptExample ie = new ThreadInterruptExample();
      Producer producer = ie.new Producer(queue);
      Consumer consumer = ie.new Consumer(queue, producer);
      producer.start();
      consumer.start();
      Thread.sleep(1000);
      producer.cancel();
      consumer.cancel();
  }
  
  class BaseQueue extends Thread {
      protected final BlockingQueue&lt;String&gt; queue;
  
      public BaseQueue(BlockingQueue&lt;String&gt; queue) {
          this.queue = queue;
      }
  
      public void cancel() {
          System.out.println(this.getName() + " - Shutting down");
          interrupt();
      }
  }
  
  class Producer extends BaseQueue {
      private final List&lt;String&gt; messages = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q",
              "r", "s", "t", "u", "v", "w", "x", "y", "z");
  
      public Producer(BlockingQueue&lt;String&gt; queue) {
          super(queue);
          this.setName("Producer");
      }
  
      public void run() {
          try {
              for (String message : messages) {
                  System.out.println(this.getName() + ": Sending " + message);
                  queue.put(message);
              }
          } catch (InterruptedException e) {
              System.out.println(this.getName() + " - InterruptedException occurred");
          }
      }
  }
  
  class Consumer extends BaseQueue {
      private final BaseQueue producer;
  
      public Consumer(BlockingQueue&lt;String&gt; queue, BaseQueue producerQueue) {
          super(queue);
          this.setName("Consumer");
          producer = producerQueue;
      }
  
      @Override
      public void run() {
          while (!Thread.currentThread().isInterrupted()) {
              System.out.println(this.getName() +": Consumer Running");
              String message = "";
              try {
                  Thread.sleep(1500);
                  message = queue.take();
                  System.out.println(this.getName() + ": Recevied " + message);
                  if (message.equals("pill")) {
                      producer.cancel();
                      this.cancel();
                  }
              } catch (InterruptedException e) {
                  System.out.print(this.getName() + ": Exception occurred for: " + message);
                  e.printStackTrace();
              }
          }
      }
  
  }
  </code></pre>
  
  <p>}</p>