 
                            ...
| Code Block | ||
|---|---|---|
| 
 | ||
| 
public final class CountBoxes implements Runnable {
  private static volatile int counter;
  // ...
  private final Object lock = new Object();
  @Override public void run() {
    synchronized (lock) {
      counter++;
      // ...
    }
  }
  public static void main(String[] args) {
    for(int i = 0; i < 2; i++) {
      new Thread(new CountBoxes()).start();
    }
  }
}
 | 
...
| Code Block | ||
|---|---|---|
| 
 | ||
| 
public class CountBoxes implements Runnable {
  private static int counter;
  // ...
  private static final Object lock = new Object();
  public void run() {
    synchronized (lock) {
      counter++;
      // ...
    }
  }
  // ...
}
 | 
It is unnecessary to declare the counter variable volatile when using synchronization.
...
| <ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="75a03f1b12f4bcca-6f3b4a72-47c34da5-90f79758-bb026bb8a0f48712d018781b"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | ]]></ac:plain-text-body></ac:structured-macro> | 
...