Why LongAdder Beats AtomicLong in High‑Concurrency Java Performance Tests

This article explains how Java's LongAdder works, compares its API and performance to AtomicLong, provides usage examples and a benchmark showing its superior throughput under high thread contention, while noting its higher memory cost and appropriate scenarios for adoption.

FunTester
FunTester
FunTester
Why LongAdder Beats AtomicLong in High‑Concurrency Java Performance Tests

Introduction

LongAdder maintains a set of variables whose sum represents a long value that can be updated atomically. When contention is high, the cells expand to reduce contention, offering higher throughput than AtomicLong at the cost of higher memory usage. It was introduced in JDK 1.8 and can replace AtomicLong in many cases.

Basic Methods

LongAdder provides a constructor, counting methods (increment, decrement, add), and methods to retrieve the current sum.

Constructor

LongAdder has a single no‑argument constructor that initializes the sum to zero.

/** Creates a new adder with initial sum of zero. */
public LongAdder() { }

Counting Methods

LongAdder offers four variants for adding values (increment, decrement, add, etc.). The reset() method clears the cells and base to zero, but should only be used when no concurrent updates are occurring.

/**
 * Resets variables maintaining the sum to zero. This method may be a useful
 * alternative to creating a new adder, but is only effective if there are
 * no concurrent updates. Because this method is intrinsically racy, it should
 * only be used when it is known that no threads are concurrently updating.
 */
public void reset() {
    Cell[] as = cells; Cell a;
    base = 0L;
    if (as != null) {
        for (int i = 0; i < as.length; ++i) {
            if ((a = as[i]) != null)
                a.value = 0L;
        }
    }
}

Because reset() is racy, it is rarely used; sumThenReset() can be used for periodic sampling such as QPS measurement.

Statistics

The core retrieval method is sum(), which returns the current total but not an atomic snapshot. Other methods like intValue() and longValue() delegate to sum().

/** Returns the current sum. The returned value is NOT an atomic snapshot; */
public long sum() {
    Cell[] as = cells; Cell a;
    long sum = base;
    if (as != null) {
        for (int i = 0; i < as.length; ++i) {
            if ((a = as[i]) != null)
                sum += a.value;
        }
    }
    return sum;
}

Performance Test

A simple benchmark creates 1,000 threads, each incrementing a LongAdder one million times, and measures execution time. Results show LongAdder scales much better than AtomicLong under high contention.

public static void main(String[] args) {
    LongAdder adder = new LongAdder();
    int POOL_SIZE = 1000;
    def phaser = new Phaser(1);
    time {
        POOL_SIZE.times {
            fun {
                1000000.times {
                    adder.increment();
                }
            }, phaser
        }
        phaser.arriveAndAwaitAdvance();
    }
    output(formatLong(adder.longValue()));
}

Sample output (LongAdder): 34.738 s for 1 billion increments, 19.588 s for 100 million, 7.095 s for 10 million. Corresponding AtomicLong times were significantly higher, confirming LongAdder’s advantage when contention is high. For low contention, both perform similarly, and the higher memory cost of LongAdder may not be justified.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaconcurrencyPerformance Testingmultithreadingatomiclonglongadder
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.