title: Main thread does not seem to be interrupted
body: <p>in my Java program main thread does not get interrupted. Why?</p>
  
  <pre><code>/*
  * This class counts till infinity
  */
  class infinityT extends Thread{
    int counter = 0;
    public void run(){
      try{
        while(true){
           if(counter&gt;500){
             // this class will throw compilation error here as Thread.sleep is not a valid method here as it does not extend Thread class
              Thread.sleep(1000);
            }
          System.out.println(counter++);
        } 
      }catch(InterruptedException e){
        System.out.println("infinity Interrupted: "+counter);
      }
    }
  }
  
  class interruption{
    public static void main(String args[]){
      Thread t = new Thread(new infinityT());
  
      // start the thread
      t.start();
  
      try{
        // main thread does not seem to interrupt
        Thread.currentThread().interrupt();
        Thread.sleep(2000);
      }catch(InterruptedException e){
        System.out.println("Main thread interrupted!");
      }
  
      t.interrupt();
    }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>...
  ...
  499
  500
  infinity Interrupted: 501
  </code></pre>