The same for all other atomic objects? It’s easier to explain the question for an AtomicInteger. Since more then 1 thread are accessing the reference to myInt, isn’t it possible that one thread sees the registered cached value, for example null, for this object, unless it is also declared volatile? If not how come?
Share
Not only is it not necessary, it’s actually semantically wrong.
AtomicReferenceholds the “real” reference within itself, and manages access to it using its own synchronization constructs. The JVM’s own synchronization constructs (synchronized,volatile, etc) and not used. TheAtomicReferenceobject itself should not be treated as volatile. If anything, consider making itfinal.Also consider this question –
volatilecan be considered to be an alternative to usingAtomicReference, if all you need is get and set operations.