zoukankan      html  css  js  c++  java
  • Java — — AtomicStampedReference解决多线程ABA问题

    package ldk.test;
    
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.atomic.AtomicInteger;
    import java.util.concurrent.atomic.AtomicStampedReference;
    
    /**
     * @Author: ldk
     * @Date: 2021/1/6 16:14
     * @Describe:
     */
    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 intT1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    atomicInt.compareAndSet(100, 101);
                    atomicInt.compareAndSet(101, 100);
                }
            });
    
            Thread intT2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                    }
                    boolean c3 = atomicInt.compareAndSet(100, 101);
                    System.out.println(c3); // true
                }
            });
    
            intT1.start();
            intT2.start();
            intT1.join();
            intT2.join();
    
            Thread refT1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                    }
                    System.out.println("1-1:"+atomicStampedRef.getStamp());
                    atomicStampedRef.compareAndSet(100, 101, atomicStampedRef.getStamp(), atomicStampedRef.getStamp() + 1);
                    System.out.println("1-2:"+atomicStampedRef.getStamp());
                    atomicStampedRef.compareAndSet(101, 100, atomicStampedRef.getStamp(), atomicStampedRef.getStamp() + 1);
                    System.out.println("1-3:"+atomicStampedRef.getStamp());
                }
            });
    
            Thread refT2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    int stamp = atomicStampedRef.getStamp();
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                    }
                    System.out.println("2-1:"+atomicStampedRef.getStamp());
                    boolean c3 = atomicStampedRef.compareAndSet(100, 101, stamp, stamp + 1);
                    System.out.println("2-2:"+atomicStampedRef.getStamp());
    
                    System.out.println(c3); // false
                }
            });
    
            refT1.start();
            refT2.start();
        }
    }

    未使用AtomicStampedReference之前,没有版本的概念,值再怎么变化,操作是可以成功的。

    使用AtomicStampedReference时候,有了版本的概念,版本一旦发生变化,操作就会失败。

     

  • 相关阅读:
    Vue介绍
    【Ribbon篇四】自定义负载均衡策略(4)
    【Eureka篇三】Eureka比Zookeeper好在哪里?(8)
    【Ribbon篇四】Ribbon核心组件IRule(3)
    【Eureka篇三】Eureka常用配置说明(7)
    【Eureka篇三】Eureka如何管理服务调用(6)
    【Ribbon篇四】Ribbon初步配置(2)
    【Ribbon篇四】Ribbon介绍(1)
    【Eureka篇三】Eureka集群配置(5)
    【Eureka篇三】Eureka服务发现(4)
  • 原文地址:https://www.cnblogs.com/dk1024/p/14246405.html
Copyright © 2011-2022 走看看