下面列出了java.util.concurrent.atomic.AtomicBoolean#weakCompareAndSet() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* repeated weakCompareAndSet succeeds in changing value when equal
* to expected
*/
public void testWeakCompareAndSet() {
AtomicBoolean ai = new AtomicBoolean(true);
do {} while (!ai.weakCompareAndSet(true, false));
assertFalse(ai.get());
do {} while (!ai.weakCompareAndSet(false, false));
assertFalse(ai.get());
do {} while (!ai.weakCompareAndSet(false, true));
assertTrue(ai.get());
}
/**
* repeated weakCompareAndSet succeeds in changing value when equal
* to expected
*/
public void testWeakCompareAndSet() {
AtomicBoolean ai = new AtomicBoolean(true);
do {} while (!ai.weakCompareAndSet(true, false));
assertFalse(ai.get());
do {} while (!ai.weakCompareAndSet(false, false));
assertFalse(ai.get());
do {} while (!ai.weakCompareAndSet(false, true));
assertTrue(ai.get());
}