Versions Compared

Key

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

Declaring a shared mutable variable volatile ensures the visibility of the latest updates on it across other threads but does not guarantee the atomicity of composite operations. For example, the variable increment operation consisting of the sequence read-modify-write is not atomic even when the variable is declared volatile.

Wiki Markup
"Sequential consistency and/or freedom from data races still allows errors arising from groups of operations that need to be perceived atomically and are not." \[[JLS 05|AA. Java References#JLS 05]\]. In such cases, the {{java.util.concurrent}} utilities must be used to atomically manipulate
the
 a shared variable. If the utilities do not provide the required atomic methods, operations that use the variable must be correctly synchronized. 

accesses to the variable must be explicitly synchronized. Note that, as with volatile, updated values are immediately visible to other threads when either one of these two techniques are is used. Synchronization provides a way to safely share object state across multiple threads without the need to reason about reordering, compiler optimizations and hardware specific behavior.

...