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