...
- A write to a variable does not depend on its current value.
- A write to a variable does not depend on the result of any non-atomic compound operations involving reads and writes of other variables. (For more information, see CON02 VNA02-J. Ensure that compound operations on shared variables are atomic.)
Wiki Markup |
---|
The first condition can be relaxed when you can be sure that only one thread will ever update the value of the variable \[[Goetz 06|AA. Java References#Goetz 06]\]. However, code that relies on an invariant such as single-thread confinement to be true at all times is error-prone and difficult to maintain. This behavior is permissible under this guideline but not recommended. |
...
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 a variable's new value depends on its current value. For more information, see CON02 VNA02-J. Ensure that compound operations on shared variables are atomic.
Compliance with CON07-J. Use private final lock objects to synchronize classes that may interact with untrusted code can reduce the likelihood of misuse by ensuring that untrusted callers cannot access the lock object.
...