import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Frame;
import Threader;

public class GreatRace extends java.applet.Applet implements Runnable{
  Threader theRacers[];
  static int racerCount = 3;
  Thread    theThreads[];
  Thread    thisThread;
  static boolean inApplet=true;
  int    numberofThreadsAtStart;

  public void init(){
    //we will use this later to see if all our Threads have died.
    numberofThreadsAtStart = Thread.activeCount();

    //Specify the layout. We will be adding all of the racers one on top
    //of the other.

    setLayout(new GridLayout(racerCount,1));

    //Specify the number of racers in this race, and make the arrays for the
    //Threaders and the actual threads the proper size.
    theRacers = new Threader [racerCount];
    theThreads = new Thread[racerCount];

    //Create a new Thread for each racer, and add it to the panel.
    for (int x=0;x<racerCount;x++){
      theRacers[x]=new Threader ("Racer #"+x);
      theRacers[x].setSize(getSize().width,getSize().height/racerCount);
      add (theRacers[x]);
      theThreads[x]=new Thread(theRacers[x]);
    	theThreads[x].setPriority(Thread.MIN_PRIORITY+2*x);
    }
  }

  public void start(){
    //Start all of the racing threads
    for (int x=0;x<racerCount;x++)
      theThreads[x].start();

    //Create a thread of our own. We will use this to monitor the state of
    //the racers and determine when we should quit altogether.
    thisThread= new Thread (this);
    thisThread.start();
  }


  public void stop(){    
    for (int x= 0;x<theRacers.length;x++){
      theRacers[x].stop();
    }
  }
  
  public void run(){
    //Loop around until all of the racers have finished the race.
    while(Thread.activeCount()>numberofThreadsAtStart+2){
      try{
        thisThread.sleep(100);
      }  catch (InterruptedException e){
        System.out.println("thisThread was interrupted");
      }
    }

    //Once the race is done, end the program.
    if (inApplet){
      stop();
      destroy();
    }
    else
      System.exit(0);
  }


  public static void main (String argv[]){
    inApplet=false;

    //Check to see if the number of racers has been specified on the command line.
    if (argv.length>0)
      racerCount = Integer.parseInt(argv[0]);

    //Create a new frame and place the race in it.
    Frame theFrame = new Frame("The Great Thread Race");
    GreatRace theRace = new GreatRace();
    theFrame.setSize(400,200);
    theFrame.add ("Center",theRace);
    theFrame.show();
    theRace.init();
    theFrame.pack();
    theRace.start();
  }

}//end class GreatRace. 
