next up previous contents
Next: Threads: Runnable & sleep() Up: Java Notes Previous: Threads: Start & Run

Threads Example 1

/**
 *  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.
 */

class ThreadTest1 extends Thread{

  // Run method must be overridden and will be called
  // when the thread is started. 
  // Run started by start()
  public void run(){
	System.out.println("Other thread has name: " + getName());
	System.out.println(getName() + " has priority "+ getPriority()) ;
	for (int i=0;i<10;i++)
	  System.out.println(getName() + ": " + (i+1));
    }

  public static void main(String[] args){
	// Create one object to run as a separate thread
	// and one object to be run by the main program
	// (the default program thread).
	ThreadTest1 test = new ThreadTest1();//Thread-1
	Thread t = new ThreadTest1() ;//Thread-2

	System.out.println("Main thread has name: " + test.getName());
	System.out.println(test.getName() + " has priority " + test.getPriority()) ;
	for (int i=0;i<10;i++)
	  System.out.println(test.getName() + ": " +(i+1));
	t.start();
  }
}



Ananda Amatya
9/15/1999