/*
 *  ==============================================================
 *  Counter.java : A suclass of Thread that counts up to a limit 
 *  with random pauses in between each count.
 * 
 *  Adapted from : Hall M., Core Web Programming, 1997
 *  Modified by : Vasilios Lagakos                     March, 2001
 *  ==============================================================
 */


public class Counter extends Thread {
  private static int totalNum = 0;
  private int currentNum, loopLimit;

  public Counter(int loopLimit) {
    this.loopLimit = loopLimit;
    currentNum = totalNum++;
  }

  private void pause(double seconds) {
    try { Thread.sleep(Math.round(1000.0*seconds)); }
    catch(InterruptedException ie) {}
  }

  /* When run finishes, the thread exits. */
  
  public void run() {
    for(int i=0; i<loopLimit; i++) {
      System.out.println("Counter " + currentNum
                         + ": " + i);
      pause(Math.random()); // Sleep for up to 1 second
    }
  }
}
