Does AtomicReference.compareAndSet(old,new)
guarantee
old.field
wasn't change?old
wasn't
reassign to a new object?if 2 is true, does it mean AtomicReference
is useful only with immutable objects like String
? (Because in case of mutability of old
the old.field
changes are lost with a successfully compareAndSet
)
AtomicReference
only checks reference equality, and does not check for changes to fields.
As a result, it is primarily only useful with immutable types.