I used AtomicLong many times, but I never had to use AtomicReference
It seems that AtomicReference is doing either (I copied this code from another stackoverflow question):
public synchronized boolean compareAndSet(List<Object> oldValue, List<Object> newValue) { if (this.someList == oldValue) { // someList could be changed by another thread after that compare, // and before this set this.someList = newValue; return true; } return false; }
Or
public synchronized boolean compareAndSet(List<Object> oldValue, List<Object> newValue) { if (this.someList == oldValue || this.someList.equals(oldValue)) { // someList could be changed by another thread after that compare, // and before this set this.someList = newValue; return true; } return false; }
Suppose this.someList is marked as volatile.
I am not sure what this is because javadoc and code for this class are not clear if .equals is used.
Seeing how the methods described above are not so difficult to write, has anyone ever used an AtomicReference?
java concurrency atomic
Adam gent
source share