I have simple general question about AtomicReference.
Why use AtomicReference if reference assignment is atomic in java?
Also I would like to ask if referen
The reason is that even though the reference is atomic, it is atomic in a very narrow sense.
If a thread writes a non volatile reference, what is guaranteed is that other threads will see the whole write or not see it at all (no word tearing/garbage).
But at no point it is guaranteed that any other thread will ever see it nor that they will be seen in the same order.
An AtomicReference provides much stronger guarantees (besides the CAS operations), essentially they behave like volatile: