原子引用-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’}

上一篇:java并发AtomicReference


下一篇:XCTF ics-04