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.mdexpands three layers fromUnsafe.compareAndSwaptocmpxchg; ABA is solved withAtomicStampedReference. - Atomics:
JavaAtomicClasses.mddistinguishesAtomicInteger/Long/ReferencefromLongAdder(striped accumulation, seeLongAdderVSAtomicLong.md). - Locks:
synchronized/Monitor.mdcovers object headers and monitors;ReentrantLock.md/Reentrancy.mdcovers reentrancy and AQS;LockSupport.mdcoverspark/unpark. - Coordination:
CompletableFutureAction/FutureTaskActionandThreadLocal.mdcover async and thread binding;JavaInterruptMechanismand 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: useAtomicLong.
Not Applicable and Risks
- CAS spinning wastes CPU under high contention; ABA requires version stamps.
ThreadLocalmust beremoved promptly to avoid leaks in pooled threads.- Reference semantics vary with GC implementations (G1/ZGC) and JDK versions.
Minimum Verification
- Verify two threads concurrently
compareAndSetwith only one success. - Compare
LongAddervsAtomicLongunder contention. - Check
ThreadLocalset/removeleaks in thread pools.
Evidence and Uncertainty
- Source facts:
ingest-java-concurrencycontains 15 source notes, including a 40KB CAS deep dive. - Synthesis: This page converges scattered chapters into CAS—locks—coordination.
- Unconfirmed:
Unsafeis increasingly restricted after JDK17;VarHandlereplacement was not verified in sources.
Related Pages
Sources and related documents
- ingest-java-concurrency
- AtomicBoolean: Atomic Boolean State and CAS Boundaries
- AutoCloseable: Resource Ownership and Close-Exception Semantics
- Java Production Performance Troubleshooting: A Minimal Symptom-to-Evidence Decision Tree
- JJWT 0.13.0: Explicitly Fixing the Signature Algorithm, Verifying by Key Type
Research notes
- Original document: src/content/note/en/java-concurrency-deepdive.md
- Source revision: f49953fb0d4bd52a39bf81167b0c4b7dd7d78a64
- Evidence status: active