I have used AtomicLong many times but I have never needed to use AtomicReference
It seems that AtomicReference does 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;
}
Assume this.someList is marked volatile.
I'm not sure really which one it is because the javadoc and the code for that class are not clear if .equals is used.
Seeing how the above methods are not exactly that hard to write has anyone ever used AtomicReference?