Versions Compared

Key

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

...

The root of the problem is that the thread that calls setFirst() and the thread that calls getFirst() lack a happens-before  relationship. A happens-before relationship exists between a thread that writes to a volatile variable and a thread that subsequently reads it. However, setFirst() and getFirst() read only read from a volatile variable—the volatile reference to the array; neither method writes to the volatile variable.

...

To ensure visibility, accessor methods may synchronize access while performing operations on nonvolatile elements of an array, whether it the array is referred to by a volatile or a nonvolatile reference. Note that the code is thread-safe even though the array reference is not volatile.

...

This noncompliant code example declares the Map instance field volatile. The instance of the Map object is mutable , because of its put() method.

...