Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: expanded a CS for clarity

...

Code Block
bgColor#ccccff
class CountBoxes implements Runnable {
  static int counter;
  // ...

  static final Object lock = new Object();    
  
  public void run() {
    synchronized(lock) {
      counter++; 
      // ...
  }
  // ...
}

There is no requirement of declaring the counter variable as volatile when synchronization is used.

Compliant Solution (2) (intrinsic lock of class object)

This compliant solution uses the intrinsic lock of the class to synchronize the increment operation.

...