next up previous contents
Next: Threads: Stopping Up: Java Notes Previous: Threads Example 2.1

Threads Example 2.2

/**
 *  This code is edited from the book:
 *
 *    Winder, R and Roberts, G (1998) 
 *    Developing Java Software
 *    John Wiley & Sons.
 *
 *  It is copyright (c) 1997 Russel Winder 
 *  and Graham Roberts.
 */

import java.util.Date ;

class ThreadTest2 implements Runnable{
  private static Thread t=new Thread(new ThreadTest2());
  // Display the current time every second, repeating
  // forever.
  public void run(){
	Thread thisThread = Thread.currentThread();
	while (t == thisThread){
	  System.out.println(new Date()) ;
	  // Wait 1 second by calling sleep, which
	  // has to be in a try block. Any exception
	  // thrown is caught and ignored.
	  try{
		Thread.currentThread().sleep(1000) ;
	  }
	  catch (InterruptedException e){ 
	  }
	}
  }
  public static void main(String[] args){
	t.start() ;
  }
}



Ananda Amatya
9/15/1999