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. In such cases, the java.util.concurrent utilities must be used to atomically manipulate the variable. If the utilities do not provide the required atomic methods, accesses to the variable must be explicitly synchronized. Note that, as with volatile, updated values are immediately visible to other threads when these two techniques are 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.

Noncompliant Code Example

...