Understanding CAS, LongAdder, and AtomicLong in Java for High‑Concurrency Counters
This article explains the principles, advantages, and drawbacks of CAS, LongAdder, and AtomicLong in Java, illustrating why Alibaba recommends LongAdder for high‑concurrency counter scenarios, how the ABA problem arises, and how AtomicStampedReference can mitigate it.
1. Introduction
In distributed systems, counters are a common requirement, and achieving high concurrency and high availability demands an appropriate implementation. Java offers two typical counter implementations: AtomicLong and LongAdder . Alibaba’s technical report recommends using LongAdder over AtomicLong .
2. CAS (Compare‑And‑Swap)
2.1 Full name
CAS stands for Compare‑And‑Swap, an atomic operation corresponding to the CPU instruction cmpxchg .
2.2 Intuitive understanding
CAS has three operands: current value A, memory value V, and new value B.
If A equals V, the memory value V is changed to B.
If A does not equal V, the operation either retries or aborts.
The core of CAS is comparing the current value with the memory value to detect modifications.
2.3 Problems of CAS
CAS suffers from the ABA problem, where a value may be changed from 10 to 100 and back to 10, making a thread think the value was never modified.
2.4 Solving the ABA problem
Java provides AtomicStampedReference , which adds a version stamp to the value, allowing comparison of both the value and its version.
3. LongAdder
3.1 What is LongAdder
Introduced in JDK 1.8 by Doug Lea, LongAdder resides in java.util.concurrent.atomic . It achieves higher performance than AtomicLong in high‑concurrency scenarios by using a segmented lock strategy, splitting a single long into multiple 16‑byte cells, each updated by an independent AtomicLong .
3.2 Why LongAdder is recommended
By distributing updates across cells, contention is reduced, and threads rarely interfere with each other, dramatically lowering the number of failed CAS retries and improving throughput. The design also allows scalability by adding more cells.
4. AtomicLong
4.1 What is AtomicLong
AtomicLong is a Java atomic class that uses CAS to ensure thread‑safe increments. Each update reads the current value and attempts a CAS; if another thread has modified the value, the operation retries.
4.2 Why AtomicLong is less suitable
In high‑concurrency scenarios, many threads contend for the same memory location, causing most CAS attempts to fail and leading to heavy spinning, which consumes CPU cycles and degrades performance.
5. Summary
Alibaba recommends LongAdder because:
High‑concurrency performance: segmented locks avoid contention.
Scalability: more cells can be added as needed.
Ease of understanding and maintenance compared to complex lock‑based solutions.
Better fit for distributed systems that require fast, reliable counters.
Choosing between LongAdder and AtomicLong should depend on the specific workload and memory considerations.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.