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

全部评论

相关推荐

09-19 12:15
门头沟学院 Java
迷茫的大四🐶:这下是真的打牌了,我可以用感谢信和佬一起打牌吗
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

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