<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">public class SummationJob implements Runnable { 
    
  public static       int X        = 0;
  public static       int NDONE    = 0;
  public static final int NTHREADS = 2;
 
  /** Increments X 1000 times. */
  public void run() {
    for(int k=0; k&lt;1000; k++) {
       X = X + 1; // WARNING: RACE CONDITION
    }
    NDONE += 1; // (WARNING: ANOTHER LESSER RACE CONDITION)
  }
  
  /** Launches NTHREADS SummationJob objects that try to increment X to NTHREADS*1000 */
  public static void main(String[] args) {
    try {
      Thread[] threads = new Thread[NTHREADS];
      for(int k=0; k&lt;NTHREADS; k++) 
         threads[k] = new Thread(new SummationJob());
        
      for(int k=0; k&lt;NTHREADS; k++) 
          threads[k].start();
      
      while(NDONE &lt; NTHREADS) Thread.sleep(100);
      
      System.out.println("X="+X);
      
    }catch(Exception e) {
       e.printStackTrace();
       System.out.println("OOPS"+e);
    }
  }
}
</pre></body></html>