【详解】Java高并发值AtomicReference

AtomicReference

提供了引用变量的读写原子性操作。

提供了如下的方法:

  • compareAndSet(V expect, V update)
  • getAndSet(V newValue)
  • lazySet(V newValue)
  • set(V newValue)
  • get()

举例

public class AtomicReferenceTest {

    private static AtomicReference<Simple> reference
            = new AtomicReference<>(new Simple("Alex",15));

    public static void main(String[] args) {
        boolean b = reference.compareAndSet(new Simple("Alex", 15), new Simple("aada", 15));
        System.out.println(b);
    }

    static class Simple{

        private String name;
        private int age;

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        public Simple(String name,int age){
            this.name = name;
            this.age = age;
        }
    }

}

结果

false

全部评论

相关推荐

zYvv:双一流加大加粗再标红,然后广投。主要是获奖荣誉不够,建议开始不用追求大厂,去别的厂子刷下实习。
点赞 评论 收藏
分享
练习生懒羊羊:开飞机把这个公司创飞吧
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务