
/**
 *  Does some processing (simulated by sleep), print the id and
 *  current state. 
 *  The thread is implemented through inheritance. */
public class Counter extends Thread
{
  /** The total number of counting threads. */
  public static int totalNum=0;
  
  /** The counters id. */
  private int currentNum;
  /** Loop counts to maximum. */
  private int loopLimit;

  /** The loop limit the the number of units of time. */
  public Counter(int loopLimit)
  {
    this.currentNum=totalNum;
    ++totalNum;
    this.loopLimit = loopLimit;
  }

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

  /** The main body of code. */
  public void run()
  {
    for (int i=0; i<loopLimit; ++i)
    {
      System.out.println("Counter id:" + currentNum + " i: " + i);
      pause(Math.random());
    }
  }

}


    

