• java并发AtomicReference


    java并发AtomicReference

    AtomicReference的作用

    已经介绍过AtomicInteger,AtomicIntegerArray,AtomicReference是针对对象的。
    他通过CAS和volatile保证了对象操作的原子性和可见性,既然是引用,就会造成引用对象的变化。

        public AtomicReference(V initialValue) {
            value = initialValue;
        }
    

    如果value改变了对象的属性,initialValue的属性也会改变
    CAS函数的实现

        /**
         * Atomically sets the value to the given updated value
         * if the current value {@code ==} the expected value.
         * @param expect the expected value
         * @param update the new value
         * @return {@code true} if successful. False return indicates that
         * the actual value was not equal to the expected value.
         */
        public final boolean compareAndSet(V expect, V update) {
            return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
        }
    

    例子

    package javalearn.javabase.thread.atomic;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.ToString;
    import lombok.extern.slf4j.Slf4j;
    
    import java.util.concurrent.atomic.AtomicReference;
    
    @Slf4j
    public class AtomicReferanceTest {
        public static void main(String[] args) {
            Persion p1 = new Persion("jack", 1);
            Persion p2 = new Persion("linda", 2);
            Persion p3 = new Persion("tony", 3);
            log.info("persion 3 :{}",p3.toString());
    
            AtomicReference<Persion> atomicReference =new AtomicReference(p3);
            log.info("AtomicReference is :{}",atomicReference.toString());
            atomicReference.compareAndSet(p3,p1);
            log.info("AtomicReference is :{}",atomicReference.toString());
            log.info("persion 3 :{}",p3.toString());
            atomicReference.set(p2);
            log.info("AtomicReference is :{}",atomicReference.toString());
            log.info("persion 3 :{}",p3.toString());
            atomicReference.get().setId(33);
            log.info("AtomicReference is :{}",atomicReference.toString());
            log.info("AtomicReference is :{}",atomicReference.toString());
            log.info("persion 3 :{}",p3.toString());
            log.info("persion 2 :{}",p2.toString());
    
        }
    
        @AllArgsConstructor
        @Data
        @ToString
        static class Persion {
            private String name;
            private int id;
    
        }
    }
    
    

    结果

    
    13:34:22.313 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - persion 3 :AtomicReferanceTest.Persion(name=tony, id=3)
    13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - AtomicReference is :AtomicReferanceTest.Persion(name=tony, id=3)
    13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - AtomicReference is :AtomicReferanceTest.Persion(name=jack, id=1)
    13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - persion 3 :AtomicReferanceTest.Persion(name=tony, id=3)
    13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - AtomicReference is :AtomicReferanceTest.Persion(name=linda, id=2)
    13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - persion 3 :AtomicReferanceTest.Persion(name=tony, id=3)
    13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - AtomicReference is :AtomicReferanceTest.Persion(name=linda, id=33)
    13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - AtomicReference is :AtomicReferanceTest.Persion(name=linda, id=33)
    13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - persion 3 :AtomicReferanceTest.Persion(name=tony, id=3)
    13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - persion 2 :AtomicReferanceTest.Persion(name=linda, id=33)
    
  • 相关阅读:
    Inno Setup入门(一)——最简单的安装脚本
    inno setup基础使用教程
    Inno Setup入门(八)——有选择性的安装文件
    Inno Setup入门(三)——指定压缩方式
    Inno Setup入门(七)——提供安装语言选项
    Inno Setup 详解中文资料
    Inno Setup入门(五)——添加readme文件
    Inno Setup 替换安装程序和卸载程序的图标
    用Inno Setup来解决.NetFramework安装问题
    写个类操作窗口(句柄操作)
  • 原文地址:https://www.cnblogs.com/JuncaiF/p/11298026.html
Copyright © 2020-2023  润新知