AtomicReference.compareAndSet()用于确定什么? [英] What does AtomicReference.compareAndSet() use for determination?

查看:2191
本文介绍了AtomicReference.compareAndSet()用于确定什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有以下类别

  public class AccessStatistics {
private final int noPages,noErrors;
public AccessStatistics(int noPages,int noErrors){
this.noPages = noPages;
this.noErrors = noErrors;
}
public int getNoPages(){return noPages; }
public int getNoErrors(){return noErrors; }
}

并执行以下代码

  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));
}

在最后一行 while(!stats.compareAndSet (prev,newValue)) compareAndSet 方法 之间如何确定< c $ c> prev 和 newValue ?是否需要 AccessStatistics 类才能实现 equals()方法?如果不是,为什么? javadoc对 AtomicReference.compareAndSet

声明以下内容:


如果当前值==期望值,则给定更新值。


...但是这个断言看起来很普遍,如果在AtomicReference中包装的类需要实现equals(),那么对于更多的对象来说,复杂比 AccessStatistics 我认为可能更快地同步更新对象的方法,而不使用AtomicReference。

解决方案

它比较的refrerences完全一样,如果你使用==运算符。这意味着引用必须指向同一个实例。不使用Object.equals()。


Say you have the following class

public class AccessStatistics {
  private final int noPages, noErrors;
  public AccessStatistics(int noPages, int noErrors) {
    this.noPages = noPages;
    this.noErrors = noErrors;
  }
  public int getNoPages() { return noPages; }
  public int getNoErrors() { return noErrors; }
}

and you execute the following code

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));
}

In the last line while (!stats.compareAndSet(prev, newValue)) how does the compareAndSet method determine equality between prev and newValue? Is the AccessStatistics class required to implement an equals() method? If not, why? The javadoc states the following for AtomicReference.compareAndSet

Atomically sets the value to the given updated value if the current value == the expected value.

... but this assertion seems very general and the tutorials i've read on AtomicReference never suggest implementing an equals() for a class wrapped in an AtomicReference.

If classes wrapped in AtomicReference are required to implement equals() then for objects more complex than AccessStatistics I'm thinking it may be faster to synchronize methods that update the object and not use AtomicReference.

解决方案

It compares the refrerences exactly as if you had used the == operator. That means that the references must be pointing to the same instance. Object.equals() is not used.

这篇关于AtomicReference.compareAndSet()用于确定什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆