java.util.concurrent.atomic · java.base · since Java 1.5
AtomicReference
public class AtomicReference<V> implements SerializableAtomic object reference: CAS-swap immutable snapshots to update multi-field state without locks — the heart of nonblocking data structures.
Key methods
boolean compareAndSet(V expect, V update) | CAS on the reference. |
V updateAndGet(UnaryOperator<V>) | Atomic functional swap. |
V getAndSet(V newValue) | Swap and return old. |
Example
AtomicReference<Range> range = new AtomicReference<>(new Range(0, 10));
range.updateAndGet(r -> r.withUpper(20)); // multi-field invariant in ONE CAS