• Java线程ABA问题


    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.atomic.AtomicReference;
    import java.util.concurrent.atomic.AtomicStampedReference;
    
    public class ABADemo {
        static AtomicReference<Integer> atomicReference = new AtomicReference<>(100);
        static AtomicStampedReference<Integer> atomicStampedReference=
                new AtomicStampedReference<>(100,1);
    
        public static void main(String[] args) {
    
            System.out.println("==============ABA问题的产生================");
            new Thread(() -> {
                atomicReference.compareAndSet(100, 101);
                atomicReference.compareAndSet(101, 100);
            }, "t1").start();
    
            new Thread(() -> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(atomicReference.compareAndSet(100,2018)+"	"
                +atomicReference.get());
            }, "t2").start();
    
            System.out.println("==============ABA问题的解决================");
    
            new Thread(()->{
                int stamp=atomicStampedReference.getStamp();
                System.out.println(Thread.currentThread().getName()
                +"	第一次的版本号:"+stamp);
    
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                atomicStampedReference.compareAndSet(100,101,
                        atomicStampedReference.getStamp(),atomicStampedReference.getStamp()+1);
                System.out.println(Thread.currentThread().getName() + "	第二次版本号" + atomicStampedReference.getStamp());
    
                atomicStampedReference.compareAndSet(101,100,
                        atomicStampedReference.getStamp(),atomicStampedReference.getStamp()+1);
                System.out.println(Thread.currentThread().getName() + "	第三次版本号" + atomicStampedReference.getStamp());
            },"t3").start();
    
            new Thread(()->{
                int stamp=atomicStampedReference.getStamp();
                System.out.println(Thread.currentThread().getName()
                        +"	第一次的版本号:"+stamp);
    
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
                boolean result=atomicStampedReference.compareAndSet(100,2018,
                        stamp,stamp+1
                );
                System.out.println(Thread.currentThread().getName() + "	修改成功否?" + result +
                        "	当前最新实际版本号:" + atomicStampedReference.getStamp());
    
                System.out.println(Thread.currentThread().getName() + "	当前实际最新值:" +
                        atomicStampedReference.getReference());
            },"t4").start();
        }
    }
    

      

  • 相关阅读:
    js基础第七天
    js基础第六天
    js基础第四天
    js基础第3天
    js基础第二天
    js基础第一天
    第一天CSS实战培训及笔记及感想
    2015年10月23日JS笔记
    表格类选中区域复制的实现
    复杂表头
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/12416695.html
Copyright © 2020-2023  润新知