原子引用-AtomicReference
作者:互联网
原子引用-AtomicReference
class User{
String name;
Integer age;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
}
public class Test {
public static void main(String[] args) {
User a = new User("a",20);
User b = new User("b",30);
AtomicReference<User> userAtomicReference = new AtomicReference<>();
userAtomicReference.set(a);
System.out.println(userAtomicReference.compareAndSet(a, b)+" "+userAtomicReference.get().toString());
System.out.println(userAtomicReference.compareAndSet(a, b)+" "+userAtomicReference.get().toString());
}
}
控制台输出结果
true User{name=‘b’, age=‘30’}
false User{name=‘b’, age=‘30’}
标签:name,age,原子,String,userAtomicReference,User,AtomicReference,引用 来源: https://blog.csdn.net/weixin_44346035/article/details/95882725