...
Code Block | ||
---|---|---|
| ||
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.
...