...
- A write to a variable is independent from its current value.
- A write to a variable is independent from the result of any nonatomic compound operations involving reads and writes of other variables. (See rule VNA02-J. Ensure that compound operations on shared variables are atomic for more information.)
The first condition can be relaxed when you can be sure that only one thread will ever update the value of the variable \ [[Goetz 2006|AA. References#Goetz 06]\]. However, code that relies on a single-thread confinement is error prone and difficult to maintain. This design approach is permitted under this rule but is discouraged. Wiki Markup
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.
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
VNA00-J | medium | probable | medium | P8 | L2 |
Automated Detection
Some static analysis tools are capable of detecting violations of this rule.
...
CWE-667. Improper locking | |
| CWE-413. Improper resource locking |
| CWE-567. Unsynchronized access to shared data in a multithreaded context |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="54202f2a-8a16-4f6a-85e9-1da109c0d6f4"><ac:plain-text-body><![CDATA [ [[Bloch 2008AA. References#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="c1195f9c-bcb6-4c37-956f-82e18b51fda6"><ac:plain-text-body><![CDATA[ |
[[Goetz 2006AA. References#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="cfd5a088-8a1f-4282-892e-a602489851c4"><ac:plain-text-body><! [CDATA[ [[JLS 2005AA. References#JLS 05]] | 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="ade1c7af-97ca-43e3-901b-955f286b24e6"><ac:plain-text-body><![CDATA[ | ||
[[JPL 2006AA. References#JPL 06] ] | 14.10.3, The Happens-Before Relationship ]]></ac:plain-text-body></ac:structured-macro> |
...
07. Visibility and Atomicity (VNA) 07. Visibility and Atomicity (VNA)