About DavidHLPL · RHINE LAB RSS ↗
FILE X-1039 / Backend engineering

Java Concurrency Deep Dive: JUC, CAS, Atomics, Locks, and Threading

ENGINEERING NOTES

Published / DavidHLPL

The 15 JUC/Java notes in Fuwari are the most systematic concurrency series in this knowledge base, covering CAS, atomics, synchronized, Monitor, ReentrantLock, ThreadLocal, LockSupport, deadlocks, and reference types. This page strings them together centered on CAS.

Core Mechanism

  • CAS: CASDeepDive.md expands three layers from Unsafe.compareAndSwap to cmpxchg; ABA is solved with AtomicStampedReference.
  • Atomics: JavaAtomicClasses.md distinguishes AtomicInteger/Long/Reference from LongAdder (striped accumulation, see LongAdderVSAtomicLong.md).
  • Locks: synchronized/Monitor.md covers object headers and monitors; ReentrantLock.md/Reentrancy.md covers reentrancy and AQS; LockSupport.md covers park/unpark.
  • Coordination: CompletableFutureAction/FutureTaskAction and ThreadLocal.md cover async and thread binding; JavaInterruptMechanism and deadlock diagnosis cover interruption and troubleshooting.
  • References: Two articles on JDK8/17 cover strong/soft/weak/phantom reference reclamation semantics.

Applicability

  • Small lock-free critical sections and counters: use CAS/atomics; complex critical sections: use explicit locks.
  • High-contention counters: use LongAdder; low contention: use AtomicLong.

Not Applicable and Risks

  • CAS spinning wastes CPU under high contention; ABA requires version stamps.
  • ThreadLocal must be removed promptly to avoid leaks in pooled threads.
  • Reference semantics vary with GC implementations (G1/ZGC) and JDK versions.

Minimum Verification

  1. Verify two threads concurrently compareAndSet with only one success.
  2. Compare LongAdder vs AtomicLong under contention.
  3. Check ThreadLocal set/remove leaks in thread pools.

Evidence and Uncertainty

  • Source facts: ingest-java-concurrency contains 15 source notes, including a 40KB CAS deep dive.
  • Synthesis: This page converges scattered chapters into CAS—locks—coordination.
  • Unconfirmed: Unsafe is increasingly restricted after JDK17; VarHandle replacement was not verified in sources.

Research notes

  1. Original document: src/content/note/en/java-concurrency-deepdive.md
  2. Source revision: f49953fb0d4bd52a39bf81167b0c4b7dd7d78a64
  3. Evidence status: active

Original source ↗