Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: general edit #2

Programmers sometimes assume that composite operations on primitive data are atomic. However, this is not true in a multithreaded environment. Even code that does not involve a composite operation can be nonatomicunsafe for use, for instance:

Code Block
x = 3;

...

For instance, if a and b currently have the value 0, and one thread calls getSum() while another calls setValues(1, 1), then getSum() might return 0, 1, or 2. Of these, the value 1 is unacceptable; it is returned when the first thread reads a and b after the second thread has set the value of a but before it has set the value of b.

Compliant Solution

This compliant solution provides synchronizes the setValues() method so that the entire operation is atomic.

...