zoukankan      html  css  js  c++  java
  • JUC之volatile

    /**
     * @desc volatile可见性、非原子性验证
     * @Date 2019/8/19
     */
    public class VolatileDemo {
        public static void main(String[] args) {
            // 验证可见性
            seeOkVisibility();
            // 验证非原子性
            seeNonAtomic();
        }
    
        private static void seeNonAtomic() {
            MyData myData = new MyData();
            for (int i = 1; i <= 100; i++) {
                new Thread(() -> {
                    try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
                    for (int j = 1; j <= 100 ; j++) {
                        myData.addPlusPlus();
                    }
                }, "T" + i).start();
            }
            while (Thread.activeCount() > 2) {
    
            }
            System.out.println(Thread.currentThread().getName() + "	 mission is over value:" + myData.number);
        }
    
        // 设置可见性
        private static void seeOkVisibility() {
            MyData myData = new MyData();
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "	 come in");
                try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
                myData.addTO60();
                System.out.println(Thread.currentThread().getName() + "	 updated number value: " + myData.number);
            }, "AAA").start();
            while (myData.number == 0) {
    
            }
            System.out.println(Thread.currentThread().getName() + "	 mission is over");
        }
    }
    
    class MyData {
        volatile int number;
        public void addTO60() {
            this.number = 60;
        }
        public void addPlusPlus() {
            this.number ++;
        }
    }

    ABA问题

    /**
     * @desc AtomicReference ABA问题
     * @Author xw
     */
    public class ABADemo {
        static AtomicReference<Integer> atomicReference = new AtomicReference<>(100);
        static AtomicStampedReference<Integer> atomicReference2 = new AtomicStampedReference<>(100, 1);
    
        public static void main(String[] args) {
            // 基础版
            atomicReferenceABA();
            // 改良版
            atomicStampedReferenceABA();
        }
    
        private static void atomicStampedReferenceABA() {
            new Thread(() -> {
                atomicReference2.compareAndSet(100, 101, 1, atomicReference2.getStamp()+1);
                atomicReference2.compareAndSet(101, 100, 2, atomicReference2.getStamp()+1);
            }, "t1").start();
            new Thread(() -> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(atomicReference2.compareAndSet(100, 2019, 1, atomicReference2.getStamp()+1) + "	" + atomicReference.get() + "	" + atomicReference2.getStamp());
            }, "t2").start();
        }
    
        private static void atomicReferenceABA() {
            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, 2019) + "	" + atomicReference.get());
            }, "t2").start();
        }
    
    }
    DCL单例 + volatile(禁止指令重排)
    /**
     * @desc DCL单例 + volatile(禁止指令重排)
     * @Date 2019/8/20
     */
    public class SingletonDemo {
        private static volatile SingletonDemo instance = null;
    
        private SingletonDemo() {
            System.out.println(Thread.currentThread().getName() + "	 我是构造方法");
        }
    
        public static SingletonDemo getInstance() {
            if (instance == null) {
                synchronized (SingletonDemo.class) {
                    if (instance == null) {
                        instance = new SingletonDemo();
                    }
                }
            }
            return instance;
        }
    
        public static void main(String[] args) {
            for (int i = 1; i < 100; i++) {
                new Thread(() -> {
                    try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
                    SingletonDemo.getInstance();
                }, "T" + i).start();
            }
        }
    }
  • 相关阅读:
    TOP 100 SOFTWARE VENDORS (ZZ)
    数列总结函数——取余分析
    科克曼女生问题
    约瑟夫问题(猴子选王)——多种解法
    乒乓球与羽毛球不同发球规则下选手的胜率——概率论+程序分析
    Codeblocks快捷键
    常用陌生快捷键
    Dev使用技巧
    vueDemo补充
    vueDemo
  • 原文地址:https://www.cnblogs.com/ice-line/p/11385437.html
Copyright © 2011-2022 走看看