ドキュメントによると、AtomicReference.compareAndSet()メソッドについていくつか質問がありました。
現在の値==期待値の場合、原子的に値を指定された更新された値に設定します。
私が理解している限り、==
オペレーターは2つのオブジェクトのアドレスを比較しています。もしそうなら、このような例ではどのように機能しますか
private AtomicReference<AccessStatistics> stats =
new AtomicReference<AccessStatistics>(new AccessStatistics(0, 0));
public void incrementPageCount(boolean wasError) {
AccessStatistics prev, newValue;
do {
prev = stats.get();
int noPages = prev.getNoPages() + 1;
int noErrors = prev.getNoErrors;
if (wasError) {
noErrors++;
}
newValue = new AccessStatistics(noPages, noErrors);
} while (!stats.compareAndSet(prev, newValue));
}
このコードスニペットでは、jvmはどのフィールドを?AccessStatistics
で比較するかをどのように認識しcompareAndSet()
ますか?実際、Javaでオーバーライドがまったく許可されていない場合、この戦略全体がどのように機能するのか疑問に思っています==
。コメントありがとうございます!