//Listing A: Stopping a thread with Thread.interrupt()

class Example1 extends Thread {

  public static void main( String args[] ) throws Exception {

    Example1 thread = new Example1();

   System.out.println( "Starting thread..." );

   thread.start();

   Thread.sleep( 3000 );

   System.out.println( "Interrupting thread..." );

   thread.interrupt();

   Thread.sleep( 3000 );

   System.out.println( "Stopping application..." );

//   System.exit( 0 );

  }

 

  public void run() {

    while ( true ) {

     System.out.println( "Thread is running..." );

      long time = System.currentTimeMillis();

      try{
	  Thread.sleep(1000);	  
      }
      catch (InterruptedException ie){
	  break;
      }

//      while ( System.currentTimeMillis()-time < 1000 ) {
//
//      }

    }

  }

}

