有人可以向我解释一下这段代码吗?
public class Counter {
private AtomicInteger value = new AtomicInteger();
public int incrementLongVersion(){//THIS PART2!!!
int oldValue = value.get();
while (!value.compareAndSet(oldValue, oldValue+1)){
oldValue = value.get();
}
return oldValue+1;
}
}
我的意思是为什么我不能只更改此代码
!value.compareAndSet(oldValue, oldValue+1)
至
false
? 我知道永远不会出现编译错误语句但是为什么
!value.compareAndSet(oldValue, oldValue+1)
没有得到错误?是不是总是假的太对了?
或者为什么我不能清除那个while循环并返回“oldValue+1”?
谢谢并恭祝安康。