/*
   WaitNotifyAll.java
   Burton Rosenberg
   Tue Jul 22 17:31:41 EDT 1997

   A program to demonstrate wait/notify

*/

import java.io.* ;

public class WaitNotifyAll 
implements Runnable {

   final static int N = 5 ;
   final static int D = 1000 ;

   int id ;
   Object lock ;

   WaitNotifyAll( int id, Object lock ) { 
      this.id = id ; 
      this.lock = lock ;
   }

   synchronized public void run () {
       
       try {
          while (true) {
	     System.out.println( id ) ;
             synchronized ( lock ) { lock.wait() ; }
	   }
	}
        catch ( InterruptedException ie ) { }
   }


   public static void main (String [] args) {
       
        Object lock = new Object() ;

        for ( int i=0; i<N; i++ ) 
            (new Thread(new WaitNotifyAll(i,lock))).start() ;

        try {
	    while ( true ) {
	       synchronized ( lock ) { lock.notifyAll() ; }
               Thread.sleep(D) ;
	    }
        }
        catch ( InterruptedException ie ) { }
	
   }   
   
}
