I have simple general question about AtomicReference.
Why use AtomicReference if reference assignment is atomic in java?
Also I would like to ask if reference assigment is atomic in 64-bit VMs?
Do we need volatile to have reference assigment atomic?
You need it when the decision on which the creation of the new value is based can depend on the previous value of the reference. For instance when implementing some LinkedList like data structure you wan't to set the head to a new node which refers to the previous node. In the time between reading the previous node and setting head to the new one some other thread could have concurrently updated the head reference's value. If our thread would not be aware of this change, it would go lost.
The operation itself would be performed atomic on the CPU core executing it but there are no guarantees that threads on other cores will be aware of it on the next read.
My previous answer was incorrect, as explained in the comment by juancn:
Previous answer
It is necessary, mainly for
compareAndSet
andgetAndSet
methods. You cannot do this atomically otherwise (2 operations are needed).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: