我已经使用AtomicLong很多次了,但从未使用过AtomicReference
似乎AtomicReference确实做到了(我从另一个stackoverflow问题复制了此代码):
public synchronized boolean compareAndSet(List oldValue, List 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;
}
要么
public synchronized boolean compareAndSet(List oldValue, List 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;
}
假设this.someList被标记为volatile。
我不确定是哪一个,因为如果使用.equals,则该类的javadoc和代码不清楚。
看到上面的方法写起来不那么难吗,有人使用过AtomicReference吗?
作者多次使用AtomicLong,却未用过AtomicReference。文中给出两段compareAndSet方法代码,假设this.someList被标记为volatile,因javadoc和代码不清楚使用.equals的情况,不确定具体使用哪个,最后询问是否有人使用过AtomicReference。
1148

被折叠的 条评论
为什么被折叠?



