Atomic classes
public class Example {
public static void main(String[] args) {
AtomicInteger visits = new AtomicInteger();
visits.incrementAndGet(); // thread-safe ++
visits.compareAndSet(10, 11); // CAS: set 11 only if current value is 10
}
}
Use atomics when a single variable is the whole shared state. They do not replace locks for multi-field invariants.
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 2 sections
- CAS retry pattern
- VarHandle (expert awareness)
Test yourself
Answer these before moving on — recall is what makes it stick.