• 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)
    
  • 相关阅读:
    jsp三种注释方法
    MyEclipse 2015优化技巧
    查看是否安装jdk及路径
    hibernate中save()、get()、load()、update()、saveorupdate()、merge()等方法
    MyEclipse更改项目名web发布名字不改问题
    try-catch-finally try中有rerun 是否执行finally
    使用HTML制作网页
    rs(0)与rs("字段名")的区别
    金蝶K3 wise 插件二次开发与配置
    初识触发器(2)
  • 原文地址:https://www.cnblogs.com/JuncaiF/p/11298026.html
Copyright © 2020-2023  润新知