CAS的ABA问题描述 AtomicStampReference

CAS的ABA问题描述

  • 在CAS操作的时候,其他线程将当前变量的值从A改成B,又改回A;
  • CAS线程用期望值A与当前变量比较的时候,发现当前变量没有变,于是CAS就将当前变量进行了交换操作,但其实当前变量改变过,这与设计思想是不符合的;
  • ABA问题的解决思路:每次当前变量更新的时候,将当前变量的版本号加1;

AtomicStampReference示例

  • public boolean compareAndSet(
    V expectedReference,
    V newReference,
    int expectedStamp,
    int newStamp)
    • 如果当前值和expectedReference相等,并且当前stamp和expectedStamp相等,把当前值更新为newReference,当前stamp更新为newStamp,并返回true;
    • 如果不满足条件,不更新,并返回false;
 
import java.util.concurrent.TimeUnit;

import java.util.concurrent.atomic.AtomicInteger;

import java.util.concurrent.atomic.AtomicStampedReference;


public class ABA {


private static AtomicInteger atomicInt

= new AtomicInteger(100);

private static AtomicStampedReference atomicStampedRef

= new AtomicStampedReference(100, 0);


public static void main(String[] args) throws InterruptedException {


Thread intABA = new Thread(new Runnable() {

@Override

public void run() {

atomicInt.compareAndSet(100, 101);

atomicInt.compareAndSet(101, 100);

}

});


Thread intCAS = new Thread(new Runnable() {

@Override

public void run() {

try {

TimeUnit.SECONDS.sleep(1);

} catch (InterruptedException e) {}

boolean isUpdated = atomicInt.compareAndSet(100, 101);

System.out.println("Thread AtomicInteger CAS isUpdated: " + isUpdated); // true

}

});


intABA.start();

intCAS.start();

intABA.join();

intCAS.join();


Thread refABA = new Thread(() -> {

try {

TimeUnit.SECONDS.sleep(1);

} catch (InterruptedException e) {}

atomicStampedRef.compareAndSet(100, 101, atomicStampedRef.getStamp(), atomicStampedRef.getStamp() + 1);

atomicStampedRef.compareAndSet(101, 100, atomicStampedRef.getStamp(), atomicStampedRef.getStamp() + 1);

});


Thread refCAS = new Thread(() -> {

int stamp = atomicStampedRef.getStamp(); // 0

try {

TimeUnit.SECONDS.sleep(2);

} catch (InterruptedException e) {}

boolean isUpdated = atomicStampedRef.compareAndSet(100, 101, stamp, stamp + 1);

System.out.println("Thread AtomicStampedReference CAS isUpdated: " + isUpdated); // false

});


refABA.start();

refCAS.start();

}


}

输出:

Thread AtomicInteger CAS isUpdated: true
Thread AtomicStampedReference CAS isUpdated: false

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页