The set and compareAndSet act differently:
- compareAndSet: Atomically sets the value for this updated value if the current value is (==) the expected value.
- set: Set to the given value.
Does the set () method support the atomic process?
Yes. It is atomic. Because for set , only one operation is used for the new value. The following is the source code for the set method:
public final void set(long newValue) { value = newValue; }
source share