...
Wiki Markup |
---|
The first condition is sometimes relaxed when it can be ensured that only one thread ever updates the value of the variable \[[Goetz 06|AA. Java References#Goetz 06]\]. However, it is still possible for reader threads to see stale values of the variable while the writing thread is in the process of modifying its value, before writing it back. Furthermore, it is harderhard to maintain code that relies on an invariant such an invariant being as single-thread confinement to be true at all times. |
Synchronizing the code makes it easier to reason about its behavior and is frequently, a more secure approach than using volatile
. However, it is slightly more expensive and can cause deadlocks when used excessively.
...