Ranadhir Nag wrote:Is that a complete explanation of this behaviour?
It is true that I am modifying 'ref' to update its next element.
However,it is only 'tail' which is instructed to refer to a separate element down the chain.
AtomicReference is an Object. The AtomicReference has a stored value, which is a reference to a Node. When you do
You change the value value stored inside the AtomicReference object so that it points to a different Node. Since both head and tail point to the same AtomicReference they both see the same change in the value stored by that single AtomicReference Object.
Head should still keep pointing to the same 'ref'
It is. It is pointing the same 'ref' it was pointing at the beginning, and the same one tail is pointing at.
albeit a 'ref' with a modified 'next' element.
No, it is pointing at the 'ref' which is pointing at a different Node.
And,I get the correct behavior if I use a volatile instead of atomicreference
Volatile is a modifier for a variable. AtomicReference is an Object. They are two completely different things, there is no comparison or interchanging between them. It is impossible to comment on your 'volatile' code because it would have to be using a completely different bit code for assignment. If you show the code which works, then perhaps we can discuss further.