Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFcccc
final class Adder {
  private final AtomicInteger a = new AtomicInteger();
  private final AtomicInteger b = new AtomicInteger();

  public int getSum() {
    return a.get() + b.get(); 
  }

  public void setValues(int a, int b) {
    this.a.set(a);
    this.b.set(b);
  }
}

This The simple replacement of the two int fields with atomic integers in this example does not eliminate the race condition because the compound operation a + b is still non-atomic.

...