【详解】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

全部评论

相关推荐

12-24 14:26
东北大学 Java
一只乌鸦:重邮+东北,好经典的学校
最后再改一次简历
点赞 评论 收藏
分享
dian3b:挺妙的,如果上纲上线显得不合人心,但是这样以来既能监督适当摸鱼,也有一定的人文关怀。
摸鱼被leader发现了...
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

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