Why LongAdder Beats AtomicLong for High‑Concurrency Counters in Java
This article explains the principles of CAS, compares Java's AtomicLong and LongAdder counters, highlights the ABA problem, and shows why Alibaba recommends LongAdder for high‑concurrency scenarios due to its segmented lock design, better performance, scalability, and ease of use.
1. Introduction
In distributed systems, a counter is a common requirement. To achieve high concurrency and high availability, we need to choose an appropriate implementation.
In Java, the two typical counter implementations are AtomicLong and LongAdder .
Alibaba’s technical report recommends using LongAdder instead of AtomicLong .
This article introduces the principles, advantages, and disadvantages of both counters and analyzes why Alibaba prefers LongAdder.
2. CAS
2.1 Full Name
CAS stands for compare and swap , an atomic operation corresponding to the CPU instruction cmpxchg.
2.2 Simple Understanding
CAS has three operands: current value A, memory value V, and new value B.
If A equals V, the memory value is changed to B.
If A does not equal V, the operation retries or aborts.
The core of CAS is comparing the current value with the memory value to detect modifications.
2.3 Issues of CAS
CAS suffers from the ABA problem.
Example:
Thread A reads value 10.
Thread B changes it to 100.
Thread C changes it back to 10.
When thread A resumes, it sees the value unchanged and proceeds, although the value was modified twice from a global perspective.
2.4 Solving the ABA Problem
Java provides AtomicStampedReference, which adds a version stamp to the value, so the comparison includes both the value and its version.
3. LongAdder
3.1 What Is LongAdder
LongAdder, introduced in JDK 1.8 by Doug Lea, resides in java.util.concurrent.atomic. It offers better performance than AtomicLong in high‑concurrency scenarios at the cost of higher memory usage.
LongAdder is a high‑performance counter from Google’s open‑source projects. It splits a long into multiple 16‑byte cells, each updated by an independent AtomicLong, allowing threads to update different cells concurrently.
Advantages: high concurrency performance, scalability, and reduced contention. Disadvantage: more complex implementation and higher memory consumption.
3.2 Why LongAdder Is Recommended
LongAdder reduces contention by using segmented locks. It maintains a base value and an array of cells, as illustrated below:
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
