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.
...
Declaring variables as volatile
establishes a happens-before relationship such that a write to the volatile
variable is always seen by a subsequent read. These Consequently, these operations appear to be sequentially consistent with respect to each other, although the code as a whole may not be sequential consistent. Consider two threads that are executing some statements:
...
In this example, Statement 3 writes to a volatile
variable, and statement 4 in the second thread, reads the same volatile
variable. The read sees the most recent write (to the same variable v
) from statement 3. This may not be true in the happens-before order because a future read can always see the default or previous value of v
instead of the one set in the most recent write. This guarantee is provided by the sequential consistency property of volatile
accesses.
Volatile read and write operations cannot be reordered with respect to each other and in addition, as required by the JMM, volatile
read and write operations are also not reordered with respect to operations on nonvolatile variables accesses. When reading Thread 2 reads the volatile
variable , the other thread it will also see statements see the results of all the writes occurring before the write to the volatile
variable to have already executed, with prior occurrences of volatile
and nonvolatile fields assuming the assigned values.in Thread 1.
In the previous example, statement 4 also sees the statements 1 and 2 to have executed and all their operands with the most-up to date values. However, this does not mean that statements 1 and 2 are sequentially consistent with respect to each otherexecuted in the order in which they appear in the program. They may be freely reordered by the compiler. In fact, if statement 1 constituted a read of some variable x
, it could see the value of a future write to x
in statement 2.
Wiki Markup |
---|
Because the guarantees of code present before the {{volatile}} write are weaker than sequentially consistent code, {{volatile}} as a synchronization primitive, performs better. "Because no locking is involved, declaring fields as {{volatile}} is likely to be cheaper than using synchronization, or at least no more expensive. However, if {{volatile}} fields are accessed frequently inside methods, their use is likely to lead to slower performance than would locking the entire methods." \[[Lea 00|AA. Java References#Lea 00]\]. |
"Finally, note that the actual execution order of instructions and memory accesses can be in any order as long as the actions of the thread appear to that thread as if program order were followed, and provided all values read are allowed for by the memory model. This allows the programmer to fully understand the semantics of the programs they write, and it allows compiler writers and virtual machine implementors to perform complex optimizations that a simpler memory model would not permit." \[[JPL 06|AA. Java References#JPL 06]\]. These stronger volatile semantics, however, increases the cost of volatile almost to cost of synchronization. Wiki Markup
Wiki Markup |
---|
The possible reorderings between {{volatile}} and nonvolatile variables are summarized in the matrix shown below. The load and store operations correspond to read and write operations that use the variable. \[[Lea 08|AA. Java References#Lea 08]\] |
...