// demo for the interrupt() method

public class Interrupter           // main class that creates and
{                                  // interrupts a thread
  public static void main(String[] args) 
  {
    Thread worker = new Thread (new InterruptibleThread());
    worker.start();
        
    // now wait 3 seconds before interrupting it
    try { Thread.sleep(1000); } catch (InterruptedException ie) { }
        
    worker.interrupt();
  }
}

// this thread will continue to run as long as it is not interrupted
class InterruptibleThread implements Runnable 
{
  public void run() 
  {
    while (true) 
    {
      System.out.println("I'm running");
      if (Thread.currentThread().isInterrupted()); // status is unaffected
//      if (Thread.interrupted()); 	// interrupted status is cleared
      if (Thread.interrupted())
      {
	System.out.println("I'm interrupted!");
	break;
      }
    }
  }
}
