Difference between getAndSet and compareAndSet in AtomicBoolean

旧街凉风 提交于 2019-11-30 16:42:54

问题


The thread title should be self-explnatory... I'm a bit confused between the specification of below methos from AtomicBoolean class:

  • java.util.concurrent.atomic.AtomicBoolean#compareAndSet
  • java.util.concurrent.atomic.AtomicBoolean#getAndSet

My assemption is that both would result in the same behavior when used as a boolean clause in an if condition:

public class Test {
  private AtomicBoolean flag = AtomicBoolean(false);

  public void processSomeAction() {
    if (flag.getAndSet(false)) { // Shouldn't this be similar to flag.compareAndSet(false)
      // process some action
    }
  }
  //...
  private void internalMutatorMethod() {
    // do some staff then update the atomic flag
    flas.set(true);
  }
}

Assuming that I want to retrieve the current flag value and update it automaticlly, shouldn't both methods produce the same behavior?

I would much appreciate any explanations regarding how and when to use each of those if I'm missing internal differences.


回答1:


The documentation is pretty clear.

  • getAndSet --> "Atomically sets to the given value and returns the previous value."
  • compareAndSet --> "Atomically sets the value to the given updated value if the current value == the expected value."

Not surprisingly, compareAndSet takes two arguments.

In your specific case:

  • if (flag.getAndSet(false)) will set flag to false only if its previous value was true
  • That would be the equivalent of if (flag.compareAndSet(true, false))



回答2:


You can look at the code for better understanding :

public final boolean getAndSet(boolean newValue) {
    for (;;) {
        boolean current = get();
        if (compareAndSet(current, newValue))
            return current;
    }
}

In getAndSet, if the value of the boolean has changed between the time you get() the old value and the time you try to change its value, compareAndSet won't change its value. Therefore, getAndSet calls compareAndSet in a loop until the boolean is set to the new value.

As to your code example :

flag.getAndSet(false) returns the old value of the AtomicBoolean. On the other hand, flag.compareAndSet(x,false) (note there are two arguments) returns whether the AtomicBoolean was modified, or in other words, it returns whether the old value of the AtomicBoolean was x.




回答3:


When I have checked the implementation I found following

public final boolean getAndSet(boolean newValue) {
    for (;;) {
        boolean current = get();
        if (compareAndSet(current, newValue))
            return current;
    }
}

Also when checking the javadoc, compareAndSet sets value only if the comparison pass while getAndSet simply set the value and return the previous value.




回答4:


The thread is a bit old but nobody mentioned that getAndSet will be more efficient than compareAndSet. CAS is a very costly instruction (on all CPU architectures, so JVM does not matter here). So they are not really equivalent.

So regarding the OP, both methods produce the same behaviour but it will not have the same performance, use getAndSet when you can.



来源:https://stackoverflow.com/questions/28147654/difference-between-getandset-and-compareandset-in-atomicboolean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!