java并发AtomicReference
AtomicReference的作用
已经介绍过AtomicInteger,AtomicIntegerArray,AtomicReference是针对对象的。
他通过CAS和volatile保证了对象操作的原子性和可见性,既然是引用,就会造成引用对象的变化。
public AtomicReference(V initialValue) {
value = initialValue;
}
如果value改变了对象的属性,initialValue的属性也会改变
CAS函数的实现
/**
* 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);
}
例子
package javalearn.javabase.thread.atomic;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.atomic.AtomicReference;
@Slf4j
public class AtomicReferanceTest {
public static void main(String[] args) {
Persion p1 = new Persion("jack", 1);
Persion p2 = new Persion("linda", 2);
Persion p3 = new Persion("tony", 3);
log.info("persion 3 :{}",p3.toString());
AtomicReference<Persion> atomicReference =new AtomicReference(p3);
log.info("AtomicReference is :{}",atomicReference.toString());
atomicReference.compareAndSet(p3,p1);
log.info("AtomicReference is :{}",atomicReference.toString());
log.info("persion 3 :{}",p3.toString());
atomicReference.set(p2);
log.info("AtomicReference is :{}",atomicReference.toString());
log.info("persion 3 :{}",p3.toString());
atomicReference.get().setId(33);
log.info("AtomicReference is :{}",atomicReference.toString());
log.info("AtomicReference is :{}",atomicReference.toString());
log.info("persion 3 :{}",p3.toString());
log.info("persion 2 :{}",p2.toString());
}
@AllArgsConstructor
@Data
@ToString
static class Persion {
private String name;
private int id;
}
}
结果
13:34:22.313 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - persion 3 :AtomicReferanceTest.Persion(name=tony, id=3)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - AtomicReference is :AtomicReferanceTest.Persion(name=tony, id=3)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - AtomicReference is :AtomicReferanceTest.Persion(name=jack, id=1)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - persion 3 :AtomicReferanceTest.Persion(name=tony, id=3)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - AtomicReference is :AtomicReferanceTest.Persion(name=linda, id=2)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - persion 3 :AtomicReferanceTest.Persion(name=tony, id=3)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - AtomicReference is :AtomicReferanceTest.Persion(name=linda, id=33)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - AtomicReference is :AtomicReferanceTest.Persion(name=linda, id=33)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - persion 3 :AtomicReferanceTest.Persion(name=tony, id=3)
13:34:22.321 [main] INFO javalearn.javabase.thread.atomic.AtomicReferanceTest - persion 2 :AtomicReferanceTest.Persion(name=linda, id=33)