【详解】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-15 14:16
门头沟学院 Java
回家当保安:发offer的时候会背调学信网,最好不要这样。 “27届 ”和“28届以下 ”公司招聘的预期是不一样的。
实习简历求拷打
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

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