下面列出了java.util.concurrent.atomic.AtomicReferenceFieldUpdater#weakCompareAndSet() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* repeated weakCompareAndSet succeeds in changing value when equal
* to expected
*/
public void testWeakCompareAndSet() {
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
a = updaterFor("x");
x = one;
do {} while (!a.weakCompareAndSet(this, one, two));
do {} while (!a.weakCompareAndSet(this, two, m4));
assertSame(m4, a.get(this));
do {} while (!a.weakCompareAndSet(this, m4, seven));
assertSame(seven, a.get(this));
}
/**
* repeated weakCompareAndSet succeeds in changing value when equal
* to expected
*/
public void testWeakCompareAndSet() {
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
a = updaterFor("x");
x = one;
do {} while (!a.weakCompareAndSet(this, one, two));
do {} while (!a.weakCompareAndSet(this, two, m4));
assertSame(m4, a.get(this));
do {} while (!a.weakCompareAndSet(this, m4, seven));
assertSame(seven, a.get(this));
}