import java.applet.Applet;
import java.awt.*;

/**  This version is likely to work correctly
 *  <B>except</B> when  an important customer is visiting.
 *
 *  Taken from Core Web Programming, 
 *  Prentice Hall and Sun Microsystems Press,
 *  http://www.corewebprogramming.com/.
 *  &copy; 2001 Marty Hall and Larry Brown;
 *  may be freely used or adapted. 
 */

public class BuggyCounterApplet2 extends Applet
                                implements Runnable{
  private int totalNum ;
  private int loopLimit ;


  // Start method of applet, not the start method of the thread. 
  // The applet start method is called by the browser after init is
  // called. 
  public void start() {
    totalNum = 0;
    loopLimit = 5;

    Thread t;
    for(int i=0; i<3; i++) {
      t = new Thread(this);
      t.start();
    }
  }

  public void run() {
    int currentNum = totalNum++;

    System.out.println("Setting currentNum to " + currentNum);

            // WE COMMENTED THIS LINE // totalNum = totalNum + 1;

    for(int i=0; i<loopLimit; i++) {
      System.out.println("Counter " + currentNum + ": " + i);
      pause(1);
    }
  }

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

}