...
Wiki Markup |
---|
The {{toggle()}} method still requires synchronization because it performs a non-atomic operation. However, this advanced technique is brittle in most other scenarios, such as, when a getter method performs operations other than just returning the value of the {{volatile}} field. The cheap read-write lock trick offers performance advantages since the method to read a value {{getFlag()}} is not synchronized. Unless read performance is critical, this method is not recommended. \[[Goetz 06|AA. Java References#Goetz 06]\] |
The cheap read-write lock trick is also addressed in CON11-J. Do not assume that declaring an object volatile guarantees visibility of its members.
Compliant Solution (java.util.concurrent.atomic.AtomicBoolean
)
...