Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: "data race"=>"race condition"

...

The getSum() method contains a data 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.

...

This does not eliminate the data race condition because compound operation a + b is still non-atomic.

...

Any operations within the synchronized methods are now atomic with respect to other synchronized methods that lock on that object's monitor (intrinsic lock). It is now possible, for example, to add overflow checking within the synchronized getSum() method without introducing the possibility of a data race condition.

Risk Assessment

If operations on shared variables are not atomic, unexpected results can be produced. For example, information can be disclosed inadvertently because one user can receive information about other users.

...