这和使用 AtomicReference 一样安全吗?
private volatile String myMember;
public void setMyMember(String s) {
myMember = s;
}
对比
private final AtomicReference<String> myMember = new AtomicReference<>();
public void setMyMember(String s) {
while (true) {
String current = myMember.get();
if (myMember.compareAndSet(current, s))
break;
}
}