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

// This appears in Core Web Programming from
// Prentice Hall Publishers, and may be freely used
// or adapted. 1997 Marty Hall, hall@apl.jhu.edu.

/** Emulates the Counter and Counter2 classes, but
 *  this time from an applet that invokes multiple
 *  versions of its own run method. This version is
 *  likely to work correctly <B>except</B> when
 *  an important customer is visiting.
 */

public class BuggyCounterApplet extends Applet
                                implements Runnable{
  private static int totalNum = 0;
  private int loopLimit = 5;

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

  private void pause(double seconds) {
    try { Thread.sleep(Math.round(1000.0*seconds)); }
    catch(InterruptedException ie) {}
  }
  
  public void run() {
    int currentNum = totalNum;
    System.out.println("Setting currentNum to "
                       + currentNum);
    totalNum = totalNum + 1;
    for(int i=0; i<loopLimit; i++) {
      System.out.println("Counter "
                         + currentNum + ": " + i);
      pause(Math.random());
    }
  }
}

