import java.applet.Applet; import java.awt.*; /** 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 * except when an important customer is visiting. public class BuggyCounterApplet extends Applet implements Runnable{ private int totalNum = 0; private int loopLimit = 5; // 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() { 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
Buggy Counter Applet.java Demonstrates that data shared by multiple threads is candidate for a potential race condition
Permanent link to this article: http://bangla.sitestree.com/buggy-counter-applet-java-demonstrates-that-data-shared-by-multiple-threads-is-candidate-for-a-potential-race-condition/