AtomicReference提供线程安全的引用更新,通过get、set和compareAndSet等方法实现无锁操作,适用于状态切换、配置管理等场景,但需注意被引用对象本身的线程安全性,推荐结合不可变对象使用以确保整体安全。

在Java中,AtomicReference 是 java.util.concurrent.atomic 包提供的一个原子类,用于对引用类型进行线程安全的操作。它允许你以原子方式更新对象的引用,避免使用 synchronized 关键字就能实现线程安全。
AtomicReference 的基本用法
AtomicReference 可以包装任何引用类型的对象(如自定义对象、集合、字符串等),并提供一系列原子操作方法:
- get():获取当前引用值
- set(newValue):设置新引用(非原子比较)
- compareAndSet(expect, update):如果当前引用等于 expect,则更新为 update,成功返回 true
- weakCompareAndSet(...):弱版本的 CAS,不保证指令有序性(已过时,推荐使用 compareAndSet)
示例代码:
import java.util.concurrent.atomic.AtomicReference;class Person { private String name; private int age;
public Person(String name, int age) { this.name = name; this.age = age; } // getter 方法 public String getName() { return name; } public int getAge() { return age; } @Override public String toString() { return "Person{name='" + name + "', age=" + age + "}"; }}
立即学习“Java免费学习笔记(深入)”;
public class AtomicReferenceExample { public static void main(String[] args) { AtomicReference
atomicPerson = new AtomicReference( new Person("Alice", 25) ); // 获取当前值 System.out.println("Current: " + atomicPerson.get()); // 原子更新:将引用改为 Bob Person oldPerson = atomicPerson.get(); boolean success = atomicPerson.compareAndSet(oldPerson, new Person("Bob", 30)); if (success) { System.out.println("Update succeeded"); } else { System.out.println("Update failed - value changed by another thread"); } System.out.println("New value: " + atomicPerson.get()); }}
在多线程环境中使用 AtomicReference
AtomicReference 特别适合多个线程需要安全地更新共享对象引用的场景。比如状态对象、配置对象或缓存实例的切换。
NetShop网店系统下载NetShop软件特点介绍: 1、使用ASP.Net(c#)2.0、多层结构开发 2、前台设计不采用任何.NET内置控件读取数据,完全标签化模板处理,加快读取速度3、安全的数据添加删除读取操作,利用存储过程模式彻底防制SQL注入式攻击4、前台架构DIV+CSS兼容IE6,IE7,FF等,有利于搜索引挚收录5、后台内置强大的功能,整合多家网店系统的功能,加以优化。6、支持三种类型的数据库:Acces
示例:线程安全的状态切换
AtomicReferencestatus = new AtomicReference<>("INIT"); Runnable worker = () -> { for (int i = 0; i < 1000; i++) { String current; do { current = status.get(); } while (!status.compareAndSet(current, "WORKING"));
// 模拟工作 try { Thread.sleep(1); } catch (InterruptedException e) {} // 恢复状态 status.set("DONE"); }};
// 启动多个线程竞争修改状态 Thread t1 = new Thread(worker); Thread t2 = new Thread(worker); t1.start(); t2.start();
try { t1.join(); t2.join(); } catch (InterruptedException e) {}
System.out.println("Final status: " + status.get());
注意事项与最佳实践
虽然 AtomicReference 提供了原子性的引用更新,但它不保证对象内部状态的线程安全。也就是说,即使引用是原子更新的,被引用对象本身的字段仍可能需要同步保护。
- 如果要修改对象内部字段,建议使用不可变对象(immutable objects),这样每次更新都创建新实例,确保线程安全
- 避免在 compareAndSet 循环中长时间执行逻辑,防止活锁或性能下降
- 对于复杂的数据结构更新,可结合使用 AtomicReference 和 synchronized 或其他并发工具
基本上就这些。AtomicReference 是处理引用类型原子更新的一个轻量且高效的工具,特别适用于状态机、配置管理、缓存替换等场景。只要注意对象本身的可变性问题,就能安全使用。









