「记录」Java使用CAS更新对象字段值
# Student.class
public class Student{
private volatile String status;
public boolean compareAndSetStatus(String expect, String update) {
return AtomicReferenceFieldUpdater
.newUpdater(Student.class, String.class, "status")
.compareAndSet(this, expect, update);
}
}
经测试 AtomicReferenceFieldUpdater
无法避免 ABA 问题
上次更新: 2022/12/31, 03:04:26