我一直在阅读Brian Goetz的JCIP。他解释了使用CAS指令实现一个非阻塞计数器。我无法理解如何使用CAS指令发生增量。任何人都可以帮我理解这一点。CAS and Non Blocking Counter
public class CasCounter {
private SimulatedCAS value;
public int getValue() {
return value.get();
}
public int increment() {
int v;
do {
v = value.get();
}
while (v != value.compareAndSwap(v, v + 1));
return v + 1;
}
}