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.
Noncompliant Code Example
...