AtomicStampedReference

重要的参考博客:

https://blog.csdn.net/zhaozhirongfree1111/article/details/72781758

 

package _AtomicStampedReference;

import java.util.concurrent.atomic.AtomicMarkableReference;
import java.util.concurrent.atomic.AtomicStampedReference;

public class MyAtomicStampedReference {
    public static void main(String[] args) {
        //对在AtomicReference上面,通过计数的方式,解决了ABA的问题
        B b = new B();
        b.setStr("old b");
        AtomicStampedReference<B> aAtomicStampedReference = new AtomicStampedReference<B>(b,0);
        System.out.println(aAtomicStampedReference.getReference().getStr()+"" +aAtomicStampedReference.getStamp());

        //更改里面的b的值
        b.setStr("new b");
        //设置预期的类和现在的类,预期的版本号与现在的版本号,如果预期与当前的符合就更改。
        aAtomicStampedReference.compareAndSet(b,b,0,1);

        System.out.println(aAtomicStampedReference.getReference().getStr()+"" +aAtomicStampedReference.getStamp());

        //===========================================================
        B b1 = new B();
        b1.setStr("old b1");
        AtomicMarkableReference<B> atomicMarkableReference = new AtomicMarkableReference<>(b1,false);
        System.out.println(atomicMarkableReference.getReference().getStr()+",,"+atomicMarkableReference.isMarked());
        b1.setStr("new b1");
        atomicMarkableReference.compareAndSet(b1,b1,false,true);
        System.out.println(atomicMarkableReference.getReference().getStr()+",,"+atomicMarkableReference.isMarked());


    }
}
old b,0
new b,1
old b1,,false
new b1,,true

 

posted @ 2018-11-07 08:50  式微胡不归  阅读(859)  评论(0编辑  收藏  举报