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

/**  Taken from "Core Web Programming", 
 *  Prentice Hall and Sun Microsystems Press,
 *  http://www.corewebprogramming.com/.
 *  © 2001 Marty Hall and Larry Brown;
 *  may be freely used or adapted. 
 */

public class BuggyCounterApplet 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  );

     totalNum = totalNum + 1;

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

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

}