Declaring shared variables as volatile ensures visibility and limits reordering of accesses. Volatile accesses do not guarantee the atomicity of composite operations such as incrementing a variable (CON01-J. Do not assume all primitive operators that composite operations on primitive data are atomic).
Declaring variables as volatile
establishes a happens-before relationship such that a write to the volatile
variable is always seen by a subsequent read. Statements that occur before the write to the volatile
field also happen-before the read of the volatile
field.
...
The use of the volatile
keyword is inappropriate for composite operations on shared variables (CON01-J. Do not assume all primitive operators that composite operations on primitive data are atomic).
Noncompliant Code Example (visibility)
...