Reading a shared primitive variable in one thread may not yield the value of the most recent write to the variable from another thread. Consequently, the thread may observe a stale value of the shared variable. To ensure the visibility of the most recent update, either the variable must be declared volatile or the reads and writes must be synchronized.
Declaring a shared variable volatile guarantees visibility in a thread-safe manner only when both of the following conditions are met:
...
Synchronizing the code makes it easier to reason about its behavior and is frequently more secure than simply using the volatile
keyword. However, synchronization has somewhat higher performance overhead and can result in thread contention and deadlocks when used excessively.
Declaring a variable volatile or correctly synchronizing the code both guarantee that 64-bit primitive long
and double
variables will be are accessed atomically. ( For more information on sharing those variables among multiple threads, see rule VNA05-J. Ensure atomicity when reading and writing 64-bit values.)
Noncompliant Code Example (Non-volatile Flag)
...
In this compliant solution, the done
flag is declared volatile to ensure that writes are visible to other threads.
...
This compliant solution uses the intrinsic lock of the Class
object to ensure that updates become are visible to other threads.
...
Synchronization is a more secure alternative in situations where the volatile
keyword or a java.util.concurrent.atomic.Atomic*
field is inappropriate, such as if when a variable's new value depends on its current value. See rule VNA02-J. Ensure that compound operations on shared variables are atomic for more information.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="092c6b47d72bfa0e-8c52f0aa-40294ec9-9b0db457-2b346657a81031142a03a830"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. Bibliography#Bloch 08]] | Item 66. Synchronize access to shared mutable data | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4a89c5ea099ecb35-9cdaab1c-4c30421f-93baa95b-f956d9a95a76b3d0402724c2"><ac:plain-text-body><![CDATA[ | [[Goetz 2006 | AA. Bibliography#Goetz 06]] | 3.4.2, Example: Using Volatile to Publish Immutable Objects | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="316e4dcf314d4915-aaa73031-48494685-8cf1b2c9-57973bedbb81ca13657df751"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | [Chapter 17, Threads and Locks | http://java.sun.com/docs/books/jls/third_edition/html/memory.html]]]></ac:plain-text-body></ac:structured-macro> |
| §17.4.5, Happens-Before Order | |||
| §17.4.3, Programs and Program Order | |||
| §17.4.8, Executions and Causality Requirements | |||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="52e001a827ad9af2-08b529d2-472f4627-b83e92cc-c450680afab1dd9e0b081b2d"><ac:plain-text-body><![CDATA[ | [[JPL 2006 | AA. Bibliography#JPL 06]] | 14.10.3, The Happens-Before Relationship | ]]></ac:plain-text-body></ac:structured-macro> |
...