AtomicReference和AtomicInteger非常类似,不同之处就在于AtomicInteger是对整数的封装,底层采用的是compareAndSwapInt实现CAS,比较的是数值是否相等,而AtomicReference则对应普通的对象引用,底层使用的是compareAndSwapObject实现CAS,比较的是两个对象的地址是否相等。也就是它可以保证你在修改对象引用时的线程安全性。
顺便说一下:引用类型的赋值是原子的。虽然虚拟机规范中说64位操作可以不是原子性的,可以分为两个32位的原子操作,但是目前商用的虚拟机几乎都实现了64位原子操作。AtomicReference底层使用的是compareAndSwapObject实现CAS,比较的是两个对象的地址(__对象属性值改变不会影响__)是否相等。也就是它可以保证你在修改对象引用时的线程安全性。
问:既然在java中引用的赋值操作本身就是是原子的,那为什么还需要AtomicReference(原子引用)?
答:如果仅需要通过赋值操作改变一个引用,确实不需要AtomicReference。
1 | // 注意volatile关键字 |
实际上相当于仅使用了AtomicReference的set()方法,看一下set()的实现:
1 | public final void set(V newValue) { |
AtomicReference的set()方法,其实就是直接赋值。
真正需要使用AtomicReference的场景是你需要CAS类操作时,由于涉及到比较、设置等多于一个的操作,需要借用Unsafe类的原子操作,比如:
/**
* Atomically sets the value to the given updated value
* if the current value {@code ==} the expected value.
* @param expect the expected value
* @param update the new value
* @return {@code true} if successful. False return indicates that
* the actual value was not equal to the expected value.
*/
public final boolean compareAndSet(V expect, V update) {
return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
}