...
The Java Language Specification also permits reads and writes of 64-bit values to be non-atomic. ( For more information, see CON25-J. Ensure atomicity when reading and writing 64-bit values.)
Noncompliant Code Example (Logical Negation)
...
As a result, the effect of the call by t2 is not reflected in flag
; the program behaves as if the call was never made toggle()
was called only once, not twice.
Noncompliant Code Example (Bitwise Negation)
...
The getSum()
method contains a race condition. For example, if a
and b
currently have the values 0 and Integer.MAX_VALUE
, respectively, and one thread calls getSum()
while another calls setValues(Integer.MAX_VALUE, 0)
, getSum()
might return 0 or Integer.MAX_VALUE
, or it might overflow and wrap. Overflow will occur when the first thread reads a
and b
, after the second thread has set the value of a
to Integer.MAX_VALUE
, but before it has set the value of b
to 0.
...
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.get() + b.get()
is still non-atomic.
Compliant Solution (Addition)
...