The set
and compareAndSet
methods act differently:
- compareAndSet : Atomically sets the value to the given updated value if the current value is equal (==) to the expected value.
- set : Sets to the given value.
Does the set() method also ensure the atomic process?
Yes. It is atomic. Because there is only one operation involved to set
the new value. Below is the source code of the set
method:
public final void set(long newValue) {
value = newValue;
}