AtomicStampedReference能解决CAS的ABA问题,下面是这个类的简单使用
public class ConcurrencyAutomicTest {
//initialRef初始值,initialStamp版本号
private static AtomicStampedReference atomicStampedReference=new AtomicStampedReference(0,0);
public static void main(String[] args){
if(atomicStampedReference.compareAndSet(0, 1, atomicStampedReference.getStamp(),atomicStampedReference.getStamp())){
log.info("" +atomicStampedReference.getReference()+" "+atomicStampedReference.getStamp());
}
if(atomicStampedReference.compareAndSet(1, 2, atomicStampedReference.getStamp(),atomicStampedReference.getStamp())){
log.info("" +atomicStampedReference.getReference()+" "+atomicStampedReference.getStamp());
}
}
}