title: Thread interrupt does not work
body: <p>This is my first attempt at creating my own thread pool. The program works except when I try stopping all the threads.
  I'm trying to interrupt all the threads but for some reason it nevers reaches this segment:</p>
  
  <p>if (this.currentThread().isInterrupted())</p>
  
  <p>This is the output:</p>
  
  <p>All tasks added<br>
  Task 5 recieved by thread 0<br>
  This is task #5<br>
  Task 4 recieved by thread 2<br>
  This is task #4<br>
  Task 3 recieved by thread 1<br>
  This is task #3<br>
  All done, threadpool closed<br>
  Task 5 now being executed by thread 0<br>
  Task 2 recieved by thread 0<br>
  This is task #2<br>
  Task 3 now being executed by thread 1<br>
  Task 1 recieved by thread 1<br>
  This is task #1<br>
  Task 4 now being executed by thread 2<br>
  No available tasks...<br>
  Task 1 now being executed by thread 1<br>
  No available tasks...<br>
  Task 2 now being executed by thread 0<br>
  No available tasks...<br></p>
  
  <p>The program is supposed to stop generating output after the stopPool() method is called.</p>
  
  <p>Can anyone tell me what is going wrong here? </p>
  
  <pre><code>public class CustomThread extends Thread
  {
              private int id;
              private Task task;
              private ThreadPool threadpool;
  
              public CustomThread(int id, Task task, ThreadPool threadpool)
              {
                  this.id = id;
                  this.task = task;
                  this.threadpool = threadpool;
              }
  
              public synchronized void run()
              {
                  while(true)
                  {
                      if (this.currentThread().isInterrupted())
                      {
                          System.out.println("Thread " + id + " is halted");
                      }
                      else
                      {
                          if (threadpool.getTaskQueue().isEmpty())
                          {
                              System.out.println("No available tasks...");
                              try 
                              {
                                  this.wait();
                              } 
                              catch (InterruptedException e) 
                              {
                                  e.printStackTrace();
                              }
                          }
                          else
                          {   
                              task = threadpool.retrieveTask();
                              System.out.println("Task " + task.getID() + " recieved by thread " + id);
                              task.run();
                              task.setState(true);
                              System.out.println("Task " + task.getID() + " now being executed by thread " + id);
                          }
                      }
                  }
              }
  
              public synchronized void wakeUp()
              {
                  this.notify();
              }
  
              public int getThreadID()
              {
                  return id;
              }
  
          }
  
      import java.util.List;
      import java.util.ArrayList;
      import java.util.Stack;
      import java.util.concurrent.BlockingQueue;
      import java.util.concurrent.LinkedBlockingQueue;
  
  
      public class ThreadPool
      {
          private List&lt;CustomThread&gt; thread_list;
          private Stack&lt;Task&gt; task_queue;
  
          public ThreadPool(int threads)
          {
              thread_list = new ArrayList&lt;CustomThread&gt;();
              task_queue = new Stack&lt;Task&gt;();
              for(int i=0; i&lt;threads; i++)
              {
                  CustomThread thread = new CustomThread(i, null, this);
                  thread_list.add(thread);
                  thread.start();
              }
          }
  
          //Must use notify() to wake up an idle thread
          public synchronized void add(Task task)
          {
              task_queue.push(task);
              try
              {
                  for(CustomThread t : thread_list)
                  {
                      t.wakeUp();
                  }
  
              }
              catch (Exception e)
              {
                  e.printStackTrace();
              }
  
          }
  
          public synchronized void stopPool()
          {
              for(CustomThread t : thread_list)
              {
                  t.currentThread().interrupt();
              }
  
          }
  
          public synchronized Stack getTaskQueue()
          {
              return task_queue;
          }
  
          public synchronized Task retrieveTask()
          {
              return task_queue.pop();
          }
      }
  
  public class Task implements Runnable
  {
      private int id;
      private boolean finished = false;
      public Task(int id)
      {
          this.id = id;
      }
  
      public synchronized void run()
      {
              System.out.println("This is task #" + id);
  
              try
              {
                  Thread.sleep(1000);
              }
              catch (Exception e)
              {
                  e.printStackTrace();
              }
      }
  
      public int getID()
      {
          return id;
      }
  
      public void setState(Boolean finished)
      {
          this.finished = finished;
      }
  
      public boolean getState()
      {
          return finished;
      }
  }
  
  
  public class Init 
  {
      public static void main(String[] args)
      {
          ThreadPool threadpool = new ThreadPool(3);
          threadpool.add(new Task(1));
          threadpool.add(new Task(2));
          threadpool.add(new Task(3));
          threadpool.add(new Task(4));
          threadpool.add(new Task(5));
          System.out.println("All tasks added");
          {
              try
              {
                  Thread.sleep(1000);
              }
              catch(Exception e)
              {
                  e.printStackTrace();
              }
          }
          threadpool.stopPool();
          System.out.println("All done, threadpool closed");
      }
  
  }
  </code></pre>