Skip to content

Path 3 · lesson 5 of 6

Intermediate26 minJava 21 (OpenJDK 21.0.13) · Spring Boot 3.5

Atomics and compare-and-set

How compare-and-set updates one variable without a lock, what the retry loop costs, where ABA and LongAdder come in, and why two atomics do not make one atomic operation.

What you will learn

  • Explain compare-and-set and write the read, compute, CAS retry loop by hand
  • Use updateAndGet and accumulateAndGet, and say why the function you pass must have no side effects
  • Recognise the ABA problem and fix it with a version stamp
  • Choose between AtomicLong and LongAdder from how the value is written and read
  • Spot state that spans two variables, and replace two atomics with one immutable object or a lock
  • Tell CAS contention from lock contention in a thread dump and a profile

Before you start

The concept

counter++ on a shared field is three steps: read, add, write. Two threads can both read 5 and both write 6, and one increment is lost. A lock fixes that by letting one thread at a time run the three steps. Compare-and-set fixes it differently. It lets every thread run the steps at once, and makes the last step conditional:

A failed compare-and-set is not an error. It is information: the value you computed from is out of date. The thread reads again, computes again and tries again. No thread ever waits for another, so no thread is ever parked or blocked, and a thread that is paused by the scheduler in the middle cannot hold anyone else up.

CasLoop.javaJava
import java.util.concurrent.atomic.AtomicInteger;class CasLoop {    private final AtomicInteger value = new AtomicInteger();    /** What incrementAndGet() means, written out. */    int increment() {        while (true) {            int current = value.get();                    // 1. read            int next = current + 1;                       // 2. compute from what was read            if (value.compareAndSet(current, next)) {     // 3. write only if nobody changed it since step 1                return next;            }            // compareAndSet returned false: another thread wrote in between. Nothing was lost. Go round again.        }    }    /** The same loop, supplied by the JDK. The function must be free of side effects: it may run more than once. */    int incrementUpTo(int max) {        return value.updateAndGet(v -> v < max ? v + 1 : v);    }}

Why it is written this way

  • The loop has no lock and no wait. A thread leaves it only by winning a compare-and-set.
  • Every pass starts with a fresh read. Reusing current from the failed pass would retry with the stale value and fail for ever.
  • updateAndGet runs the same loop for you. The Java 21 documentation says the function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.
ClassHoldsTypical use
AtomicInteger, AtomicLongOne numberSequence numbers, in-flight counts, limits that are checked before they are changed
AtomicBooleanOne flagRun-once guards: if (started.compareAndSet(false, true))
AtomicReference<T>One referencePublishing an immutable snapshot, and replacing it with a modified copy
AtomicStampedReference<T>A reference and an int stamp, changed togetherA reference with a version number, to detect that it changed and changed back
LongAdderA sum spread over several cellsStatistics written by many threads and read now and then
Every one of these makes operations on a single variable atomic. None of them makes two variables change together.

Program outputjava LostUpdate.java (lab lost-updates: 8 threads, 200,000 increments each)

Real JVM output
8 threads x 200,000 increments, expected 1,600,000
...
int, no synchronisation          result   734,295   lost   865,705 (54.1%)     19 ms
volatile int                     result   951,024   lost   648,976 (40.6%)     15 ms
synchronized block               result 1,600,000   lost         0 ( 0.0%)     34 ms
AtomicInteger.incrementAndGet    result 1,600,000   lost         0 ( 0.0%)     10 ms
LongAdder.increment              result 1,600,000   lost         0 ( 0.0%)     18 ms
The unprotected int lost 865,705 of 1,600,000 increments and the volatile int lost 648,976: visibility does not make read, add, write atomic. AtomicInteger.incrementAndGet lost none and took 10 ms, against 34 ms for the synchronized block and 18 ms for LongAdder. These are single short runs on 2 CPUs. The stable result is the lost column, not the order of the last three timings.

Why it exists

A lock that is rarely contended is cheap, so atomics are not a general replacement for locks. They earn their place in three situations.

  • Very short updates on hot paths. A request counter or an in-flight gauge is touched by every request. With a lock, a thread that loses the race is parked and woken, which costs far more than the increment. With an atomic, it retries a few instructions.
  • Progress that does not depend on any one thread. A thread that holds a lock and is descheduled, or stalls in a page fault, delays every waiter. A thread paused in the middle of a CAS loop delays nobody: the others simply win.
  • Publishing immutable state. One reference to an immutable object can be replaced in a single atomic step. Readers need no lock and always see a complete object.

The limits matter just as much. Compare-and-set protects one variable. As soon as a rule connects two values, such as a window start and a count, or a lower and an upper bound, two atomics give you two correct variables and an incorrect whole. And under heavy contention a CAS loop does not wait, it spins: the cost shows up as CPU, not as blocked threads.

How it works inside

What compare-and-set is made of

An AtomicInteger is an object with one volatile int field. get() is a volatile read. compareAndSet has the memory effects of a volatile read and a volatile write, so a successful CAS is visible to every thread that reads afterwards. The JVM does not take a lock for it. It compiles the call down to an atomic compare-and-exchange operation of the processor, which is why there is no monitor and no parked thread to see in any tool.

  • compareAndSet compares with ==. For AtomicReference that means the same object, not equals. Two Long objects with the same value are different references.
  • weakCompareAndSetPlain and its relatives may fail even when the value matches. They exist for loops on some processors. Use compareAndSet unless you are writing such a loop.
  • incrementAndGet, getAndAdd and getAndSet never report failure to the caller. Any retry happens inside the JDK.
  • updateAndGet(f), getAndUpdate(f) and accumulateAndGet(x, f) run the read, compute, CAS loop around your function. When a CAS fails, the function is called again with the new value.

Program outputjava -XX:ActiveProcessorCount=2 -Xmx128m AtomicsDemo.java (parts 1 and 2)

Real JVM output
8 threads, 2 CPUs
...
1. CAS retry loop: 8 threads x 200,000 increments
   result 1,600,000   lost 0   compareAndSet calls that failed and were retried: 10,876
...
2. updateAndGet(v -> v + 1): 1,600,000 updates applied, the function was called 1,618,274 times
Part 1 is the hand-written loop from above with a counter on the failure branch. No increment was lost, and 10,876 compare-and-sets failed and were retried, about 0.7% of the 1.6 million. Part 2 counts calls of the function passed to updateAndGet: 1,618,274 calls for 1,600,000 updates. The function ran 18,274 times more than there were updates. If it had sent a message or drawn an id from a sequence, those would have happened 18,274 extra times. Other runs of the same program gave 61,027 and 62,930 failed CAS calls: the number depends on scheduling.

The ABA problem

Compare-and-set answers one question: does the variable hold the same value now as when I read it? It cannot tell whether the value changed to something else and back in between. If the value is a plain number, that usually does not matter: a balance that went from 100 to 50 and back to 100 really is 100. It matters when the value is a reference whose meaning depends on other state, the classic case being the top node of a lock-free stack.

Aba.javaJava
import java.util.concurrent.atomic.AtomicReference;import java.util.concurrent.atomic.AtomicStampedReference;class Aba {    static final class Node {        final String name;        Node next;        Node(String name) { this.name = name; }    }    /** A lock-free stack. Correct as long as a popped node object is never pushed again. */    static final class Stack {        final AtomicReference<Node> top = new AtomicReference<>();        Node pop() {            while (true) {                Node seenTop = top.get();                if (seenTop == null) return null;                Node seenNext = seenTop.next;                 // <- a thread can be paused here                if (top.compareAndSet(seenTop, seenNext)) {   // "top is still the same object" is all this checks                    return seenTop;                }            }        }    }    /** The same pop with a version number that every successful change increases. */    static final class StampedStack {        final AtomicStampedReference<Node> top = new AtomicStampedReference<>(null, 0);        Node pop() {            int[] stampHolder = new int[1];            while (true) {                Node seenTop = top.get(stampHolder);          // reference and stamp, read together                if (seenTop == null) return null;                int seenStamp = stampHolder[0];                Node seenNext = seenTop.next;                if (top.compareAndSet(seenTop, seenNext, seenStamp, seenStamp + 1)) {                    return seenTop;                           // fails if anything happened in between, even A -> C -> A                }            }        }    }}

Why it is written this way

  • In Stack.pop, a thread reads top = A and A.next = B and is then paused. Another thread pops A, pops B and pushes the node object A again. The top is A once more, so the first thread's CAS succeeds and installs B, a node that is no longer part of the stack.
  • In Java this needs the same node object to be pushed twice, for example because nodes come from a pool. A stack that allocates a new node for every push does not have this problem, because the garbage collector cannot reuse the address of an object that a thread still refers to.
  • AtomicStampedReference compares the reference and an int stamp in one step. Each successful change uses a new stamp, so A with stamp 0 and A with stamp 2 are different states.
  • The cost: the OpenJDK implementation keeps the reference and stamp in an internal pair object and creates a new one on each change.

Program outputAtomicsDemo.java, part 4

Real JVM output
4. ABA: the steps of two threads, run one after another in the unlucky order
   AtomicReference:        compareAndSet(A, B) returned true, top is now B (B was popped earlier: the stack is corrupt)
   AtomicStampedReference: compareAndSet(A, B, stamp 0, 1) returned false, top is still A with stamp 2
This part is not a race. It runs the steps of the two threads one after another, in the unlucky order, on a stack whose node A is pushed a second time. With AtomicReference the stale compare-and-set returned true and made the removed node B the top. With AtomicStampedReference the same call returned false because the stamp had moved from 0 to 2.

LongAdder: many writers, few readers

With many cores writing one AtomicLong, every write needs exclusive ownership of the cache line that holds the value, and CAS failures rise. LongAdder avoids the single hot location. It starts with one base value. When updates collide, it creates an array of cells, and each thread adds to the cell its hash selects. In OpenJDK 21 the cells are padded against sharing a cache line, and the array stops growing once it has at least as many cells as there are CPUs. sum() adds the base and all cells.

AtomicLong

  • One value. get() is exact at the moment it is read
  • Supports compareAndSet, so it can enforce a limit or hand out unique sequence numbers
  • All writers contend on one memory location
  • Small: one object with one field

LongAdder

  • A base plus cells. sum() is not an atomic snapshot: updates that happen during the sum may or may not be included
  • No compareAndSet. It can add and it can be summed, nothing else
  • Writers spread over cells when they collide
  • Uses more memory, and more as contention grows

Program outputAtomicsDemo.java, part 3

Real JVM output
3. 8 threads x 5,000,000 increments
   run 1   AtomicLong.incrementAndGet   218 ms (result 40,000,000)   LongAdder.increment   183 ms (sum 40,000,000)
   run 2   AtomicLong.incrementAndGet   202 ms (result 40,000,000)   LongAdder.increment   214 ms (sum 40,000,000)
On this machine, with 2 CPUs, the two are equal within noise: 218 and 202 ms against 183 and 214 ms for 40 million increments. That is an honest result, not a failed demo. With 2 CPUs at most two threads write at the same instant, so there is little contention to remove. The API documentation says the classes are similar under low contention and that LongAdder has significantly higher expected throughput under high contention, which needs many cores writing at once. Measure on the hardware you deploy to before you switch.

When one atomic is not enough

If a rule spans two variables, you have two correct options. Put both values in one immutable object held by an AtomicReference and replace the whole object with compare-and-set. Or guard both with a lock. The first keeps readers lock-free and costs an allocation per update. The second is simpler when the update is long or touches many fields.

Program outputAtomicsDemo.java, part 5

Real JVM output
5. Range starts at [0, 10]. One thread sets lower = 5 if 5 <= upper, the other sets upper = 4 if 4 >= lower. 20,000 rounds
   two AtomicIntegers:                     1 rounds ended with lower > upper
   one AtomicReference<Range> + CAS:       0 rounds ended with lower > upper (20,000 updates refused by the check)
The range must satisfy lower <= upper. Each thread checks the other bound and then writes its own. With two AtomicIntegers, 1 round in 20,000 ended with lower = 5 and upper = 4. The program had to line the two threads up to within microseconds to get even that. In a service this is a bug that passes every test and appears a few times a week. With one AtomicReference holding an immutable Range, the check and the write refer to the same object, a stale check makes the CAS fail, and the second thread's update is refused in all 20,000 rounds.

How CAS contention looks from outside

  • Thread dump: threads in a CAS loop are RUNNABLE. There is no BLOCKED state, no waiting to lock, no parking to wait for. Several dumps a second apart show the same threads in the same few frames.
  • JFR: no jdk.JavaMonitorEnter and no jdk.ThreadPark events come from an atomic. The cost appears in execution samples, so jfr view hot-methods shows the method that contains the loop.
  • Operating system: CPU is high and stays high as threads are added, while throughput stops rising.

Architecture

Drawing diagram…

Flowchart. If a rule connects two or more values, use one AtomicReference to an immutable record when the update is short and the state small, otherwise a lock. If the state is a single number that is only added to and read for statistics, use LongAdder. If it is a reference to an object that may be reused, use AtomicStampedReference. Otherwise use AtomicInteger, AtomicLong or AtomicReference.

flowchart TD
  A["Shared state changed by several threads"] --> B{"Does a rule connect two or more values"}
  B -->|"Yes"| C{"Is the update short and the state small"}
  C -->|"Yes"| D["AtomicReference to one immutable record, replaced with CAS"]
  C -->|"No"| E["A lock around all the values"]
  B -->|"No"| F{"Is it a number that is only added to and read for statistics"}
  F -->|"Yes"| G["LongAdder"]
  F -->|"No"| H{"Is it a reference to a reused object, such as a pooled node"}
  H -->|"Yes"| I["AtomicStampedReference"]
  H -->|"No"| J["AtomicInteger, AtomicLong or AtomicReference"]
Choosing between one atomic, a stamped reference, LongAdder, an immutable record and a lock.
  • The first question is about invariants, not performance. An atomic that protects half of a rule protects nothing.
  • LongAdder is for sums that are written much more often than they are read and where a slightly stale total is fine. A value that gates a decision, such as a concurrency limit, needs compareAndSet and so an AtomicInteger or AtomicLong.
  • A stamped reference is rarely needed in application code. If you allocate a new object for every change and never reuse one, identity already tells you whether anything changed.
  • The lock branch is not a defeat. A long update retried in a CAS loop repeats all its work on every collision, while a lock makes the losers wait once.

Step by step

Drawing diagram…

Sequence diagram. Thread A and thread B both read 5 from an AtomicInteger. A's compareAndSet from 5 to 6 succeeds. B's compareAndSet from 5 to 6 fails because the value is now 6. B reads again, gets 6, and its compareAndSet from 6 to 7 succeeds. The final value is 7 and no update was lost.

sequenceDiagram
  participant A as Thread A
  participant V as AtomicInteger value 5
  participant B as Thread B
  A->>V: get
  V-->>A: 5
  B->>V: get
  V-->>B: 5
  A->>V: compareAndSet 5 to 6
  V-->>A: true, value is now 6
  B->>V: compareAndSet 5 to 6
  V-->>B: false, value is not 5
  Note over B: nothing was written, read again
  B->>V: get
  V-->>B: 6
  B->>V: compareAndSet 6 to 7
  V-->>B: true, value is now 7
Two increments that overlap. The second thread's stale write is refused, and its retry succeeds.
  1. With a plain int, B's second step would have been an unconditional write of 6, and A's increment would have been lost. The condition on the write is the whole difference.
  2. B did its computation twice. That is the price of not waiting. For current + 1 it is nothing. For a function that builds a large object it is the allocation and the work, repeated.
  3. Neither thread was ever suspended. If the operating system had paused A between its read and its CAS, B would have succeeded at once and A would have retried later.

In a real system

A checkout service calls a partner API that allows 100 calls per second per client. Each instance has an in-memory limiter with two atomics: the start of the current window and the number of calls used. A few times a day the partner answers 429 Too Many Requests although the service's own metric says it never sent more than 100 in any second. There is nothing in a thread dump: no thread is blocked or waiting. The limiter's unit tests pass.

Program outputjava -XX:ActiveProcessorCount=2 -Xmx128m AtomicsLimiter.java

Real JVM output
limit 1 per window, 2 threads call tryAcquire() together at the start of each of 100,000 windows, 2 CPUs

two atomics (windowStart, used)
  windows that admitted 1 call: 99,972   windows that admitted 2 calls (over the limit): 28

one AtomicReference<Window> + CAS
  windows that admitted 1 call: 100,000   windows that admitted 2 calls (over the limit): 0
The same limiter logic with a limit of 1 per window and two threads that arrive together as each window opens. A correct limiter admits one of the two. The two-atomics version admitted both in 28 of 100,000 windows; an earlier run gave 9. The version that holds start and count in one immutable object behind an AtomicReference never did. A rate of 0.03% explains the incident exactly: invisible in tests, a handful of over-limit seconds per day in production.

The interleaving is short. Thread A sees the window expired, writes the new start, resets the count to 0 and increments it to 1. Thread B saw the window expired before A wrote the start. It now also writes the start and resets the count to 0, erasing A's call, and increments it to 1. Both return true. Each of the six operations was atomic.

  • Metrics. A request counter incremented by every thread and scraped every 15 seconds is the textbook LongAdder. A gauge of in-flight requests that is compared with a maximum before admitting a request must be an AtomicInteger with a CAS loop.
  • Sequence numbers. AtomicLong.getAndIncrement() gives each caller a different number. It does not give them in the order the callers finish their work.
  • Configuration snapshots. Feature flags or routing tables held as an immutable map in an AtomicReference. Writers copy, change and publish; readers call get() and never lock.
  • CPU without waiting. When a profile shows a CAS loop as the hottest method and CPU is saturated, the update is too contended for an atomic. Reduce the sharing: use LongAdder for a sum, keep per-thread or per-key state, or batch updates.

Java and Spring Boot

Two Spring Boot components from the checkout service that use atomics for what they are good at: exact single-variable decisions, statistics, and publishing an immutable snapshot. Both were compiled with javac on JDK 21 against stand-ins for the Spring annotations; they were not run inside a Spring application.

src/main/java/com/example/checkout/RequestStats.javaJava
package com.example.checkout;import java.util.concurrent.atomic.AtomicInteger;import java.util.concurrent.atomic.AtomicLong;import java.util.concurrent.atomic.LongAdder;import org.springframework.stereotype.Component;@Componentpublic class RequestStats {    private static final int MAX_IN_FLIGHT = 64;    private final LongAdder completed = new LongAdder();          // written on every request, read by a scraper    private final AtomicLong slowestNanos = new AtomicLong();    private final AtomicInteger inFlight = new AtomicInteger();   // read to make a decision, so it must be exact    /** Admits the request only while fewer than MAX_IN_FLIGHT are running. */    public boolean tryEnter() {        while (true) {            int current = inFlight.get();            if (current >= MAX_IN_FLIGHT) {                return false;            }            if (inFlight.compareAndSet(current, current + 1)) {                return true;            }        }    }    public void exit(long elapsedNanos) {        inFlight.decrementAndGet();        completed.increment();        slowestNanos.accumulateAndGet(elapsedNanos, Math::max);    }    public long completed() {        return completed.sum();    }    public long slowestNanosAndReset() {        return slowestNanos.getAndSet(0);    }    public int inFlight() {        return inFlight.get();    }}

Why it is written this way

  • tryEnter is a check followed by a write, so it is a CAS loop. if (inFlight.get() < MAX) inFlight.incrementAndGet() would let several threads pass the check together and exceed the maximum.
  • completed is a LongAdder because it is written on every request and only read by a metrics scraper, where a total that is a few requests behind is fine.
  • accumulateAndGet(elapsedNanos, Math::max) keeps the maximum without a lock. Math::max has no side effects, so being re-applied after a failed CAS is harmless.
  • getAndSet(0) reads and resets the maximum in one step. A separate get() and set(0) would drop a value recorded between the two.
  • The three fields are independent: no rule connects them. That is what makes three separate atomics correct here.

Wrong, then right

The per-instance rate limiter in front of the partner API. The incorrect version uses an atomic for each of its two values, which looks thread-safe in review because there is no plain shared field anywhere.

Incorrect implementation

src/main/java/com/example/partner/PartnerRateLimiter.javaJava
package com.example.partner;import java.time.Clock;import java.util.concurrent.atomic.AtomicInteger;import java.util.concurrent.atomic.AtomicLong;import org.springframework.stereotype.Component;/** At most LIMIT calls to the partner API per window, for this instance. */@Componentpublic class PartnerRateLimiter {    private static final int LIMIT = 100;    private static final long WINDOW_MILLIS = 1_000;    private final Clock clock;    private final AtomicLong windowStart = new AtomicLong();    private final AtomicInteger used = new AtomicInteger();    public PartnerRateLimiter(Clock clock) {        this.clock = clock;    }    public boolean tryAcquire() {        long now = clock.millis();        if (now - windowStart.get() >= WINDOW_MILLIS) {   // check on one atomic ...            windowStart.set(now);                         // ... then two separate writes            used.set(0);        }        return used.incrementAndGet() <= LIMIT;    }}

Why it is written this way

  • The rule is about two values together: used counts calls since windowStart. Nothing ties a write of one to a write of the other.
  • Between the if and the two set calls another thread can run the same branch.
  • used.set(0) is an unconditional write. It erases whatever other threads counted since they started the same window.

Why it fails

  • Two threads arrive as a window expires. Both read the old windowStart and both enter the if. The first resets used and counts its call. The second then resets used again, erasing that call, and counts its own. Calls that were admitted are no longer counted, so more than LIMIT get through in that window.
  • A thread can also be descheduled between windowStart.set(now) and used.set(0). When it runs again, possibly late in the window, it wipes the whole count and the window gets a second full quota.
  • In the real run above, the two-thread version of this race admitted both callers in 28 of 100,000 windows. The production symptom is occasional 429 responses from the partner, at window boundaries under load, while the limiter's own counter never shows more than the limit.
  • No tool flags it. Nothing blocks, so thread dumps and lock profiles are clean, and a test with one thread, or with threads that do not hit the boundary together, always passes.

Corrected implementation

src/main/java/com/example/partner/PartnerRateLimiter.javaJava
package com.example.partner;import java.time.Clock;import java.util.concurrent.atomic.AtomicReference;import org.springframework.stereotype.Component;/** At most LIMIT calls to the partner API per window, for this instance. */@Componentpublic class PartnerRateLimiter {    private static final int LIMIT = 100;    private static final long WINDOW_MILLIS = 1_000;    /** Both values in one immutable object, so they can only change together. */    private record Window(long start, int used) { }    private final Clock clock;    private final AtomicReference<Window> window = new AtomicReference<>(new Window(0, 0));    public PartnerRateLimiter(Clock clock) {        this.clock = clock;    }    public boolean tryAcquire() {        long now = clock.millis();        while (true) {            Window current = window.get();            Window next = now - current.start() >= WINDOW_MILLIS                    ? new Window(now, 1)                               // first call of a new window                    : new Window(current.start(), current.used() + 1);            if (next.used() > LIMIT) {                return false;                                          // full: nothing to write            }            if (window.compareAndSet(current, next)) {                return true;                                           // nobody changed it since we read it            }            // lost the race: another thread replaced the window. Read it again and decide again.        }    }}

Why it is written this way

  • Window is an immutable record. A thread decides from one Window object and proposes one new Window object.
  • compareAndSet(current, next) succeeds only if the limiter still holds the exact object the decision was based on. If another thread started a new window or counted a call, the CAS fails and the decision is made again from the new state.
  • A full window returns false without writing, so rejected calls do not contend with each other.
  • now is read once, before the loop. Retries are a few nanoseconds apart, so the same timestamp is close enough, and the loop stays free of side effects.

Trade-offs

  • Every admitted call allocates a small Window object. The object is published to other threads, so escape analysis cannot remove it. At thousands of calls per second this is minor; in a path taken millions of times per second, measure it.
  • Under heavy contention threads retry instead of waiting. The loop is short, but CPU rises with the number of threads that collide. A ReentrantLock or synchronized around two plain fields is equally correct and easier to extend when the update grows.
  • The algorithm is still a fixed window: 100 calls at the end of one second and 100 at the start of the next are both allowed. That is a property of the algorithm, not of the atomics.
  • The limit is per instance. With four instances the partner sees up to four times the limit unless each gets a quarter or the limiter moves to shared storage.

Hands-on exercise

Lose an update, then make the write conditional

Step two threads through read, add and write on a plain counter until an update is lost, then switch the counter to an atomic and watch the stale write being refused and retried.

Steps · 0 of 6 done

Read, add, write: plain against atomic

Simulation · not a real JVM

A simulation of two threads incrementing one counter. Choose which thread takes the next step, first on a plain int, then with compare-and-set.

Take it further in the simulation: Lost updates: counter++ is three steps

What can go wrong

Check on one atomic, act on another
Symptom
A limit is exceeded by a small amount, or two related numbers disagree, a few times per million operations. Tests pass. Thread dumps show nothing.
Cause
Each call is atomic, the sequence is not. Another thread runs between the check and the write, or between two writes that belong together.
Fix
Hold the related values in one immutable record inside an AtomicReference and replace it with compareAndSet, or guard them with one lock.
Side effects inside updateAndGet
Symptom
Under load, audit events, notifications or ids generated inside the update function appear more often than the number of successful updates.
Cause
The function is re-applied whenever the CAS fails. In the real run it was called 1,618,274 times for 1,600,000 updates.
Fix
Keep the function pure. Compute the new value in it, and perform the side effect once, after updateAndGet returns.
compareAndSet on boxed numbers
Symptom
AtomicReference<Long>.compareAndSet(expected, next) returns false although the printed values are equal, and a retry loop around it spins for ever.
Cause
The comparison is by reference (==). Two Long objects holding the same number are usually different objects.
Fix
Use AtomicLong or AtomicInteger for numbers. With AtomicReference, pass the very object returned by get() as the expected value.
Using LongAdder for a decision
Symptom
A concurrency or quota limit built on adder.sum() < max followed by adder.increment() lets too many through.
Cause
sum() is not an atomic snapshot and LongAdder has no compare-and-set, so check and add cannot be made one step.
Fix
Use AtomicInteger or AtomicLong with a CAS loop for anything that gates a decision. Keep LongAdder for statistics.
A long computation in a CAS loop
Symptom
CPU is saturated and throughput falls as threads are added. A profile shows the update function and allocation at the top. No thread is blocked.
Cause
Every collision throws away the loser's whole computation and repeats it. The longer the computation, the more likely a collision, so retries feed on themselves.
Fix
Shorten what happens between read and CAS, split the state so that threads touch different variables, or use a lock so that losers wait once.

How to troubleshoot

High CPU, flat throughput, and no BLOCKED or parked application threads

Check

jcmd <pid> Thread.print   three times, a second apart, and compare the top frames of the RUNNABLE threads
How to read it
The same threads RUNNABLE in the same method with compareAndSet, updateAndGet or accumulateAndGet nearby means a contended CAS loop. Lock contention would show BLOCKED or WAITING (parking) instead.
Need to confirm where the CPU goes

Check

jcmd <pid> JFR.start settings=profile duration=60s filename=/tmp/cas.jfr   then   jfr view hot-methods /tmp/cas.jfr
How to read it
A method containing the retry loop, or the function passed to updateAndGet, at the top of the list confirms it. jfr view contention-by-site will be empty: atomics produce no lock events.
Suspect a counter is losing updates

Check

java -XX:ActiveProcessorCount=2 LostUpdate.java   (lab lost-updates), or the same idea as a test: N threads, M increments each, assert the total is N x M
How to read it
A total below N x M means a read, add, write sequence is not atomic. Repeat the test many times: one clean run proves little, as the 1-in-20,000 result in this lesson shows.
A limit or invariant is violated rarely and nothing shows in dumps

Check

Search the class for two or more Atomic fields used in one method: grep -n 'Atomic' src/main/java/com/example/partner/PartnerRateLimiter.java
How to read it
A method that reads one atomic and writes another, or writes two, has a window between the calls. Decide which values belong together and move them into one object or under one lock.

Key takeaways

  • Compare-and-set makes the write conditional on the value being unchanged since it was read. A failed CAS loses nothing: read again and retry.
  • Atomics never block. Contention shows up as retries and CPU, with RUNNABLE threads and no lock events, not as waiting.
  • The function given to updateAndGet or accumulateAndGet can run more than once per update. Keep side effects out of it.
  • CAS compares identity. ABA needs a reused object; a version stamp in AtomicStampedReference detects it.
  • LongAdder is for sums written by many threads and read occasionally. Its sum() is not a snapshot and it cannot enforce a limit. Its advantage needs real contention: on 2 CPUs it measured the same as AtomicLong.
  • One atomic protects one variable. When a rule spans two, use one immutable object in an AtomicReference, or a lock.

Practical check

Small scenarios, one decision each. Pick what you would do, then read why.

Scenario 1 of 3

A warehouse service keeps available and reserved as two AtomicIntegers. reserve(n) runs if (available.get() >= n) { available.addAndGet(-n); reserved.addAndGet(n); }. Under load, available is sometimes negative, and a nightly audit finds that available + reserved briefly differs from the stock total.

Which change makes the reservation correct?

Scenario 2 of 3

A gateway on a 32-core host counts requests per route in AtomicLongs. A JFR recording at peak shows 30% of execution samples in the method that calls incrementAndGet on the busiest route's counter. No threads are BLOCKED. The counters are read once every 15 seconds by a metrics scraper and by nothing else.

What is the fitting change?

Scenario 3 of 3

An order service assigns invoice numbers with last.updateAndGet(n -> { audit.publish(new NumberIssued(n + 1)); return n + 1; }) on an AtomicLong. Invoice numbers are unique and gapless. At peak load the audit topic contains some numbers twice, and a few NumberIssued events for numbers that went to a different order.

What explains it and what is the fix?

References

References: Java SE 21 API: package java.util.concurrent.atomic ↗ · Java SE 21 API: AtomicInteger (compareAndSet, updateAndGet, accumulateAndGet) ↗ · Java SE 21 API: VarHandle (memory semantics of compareAndSet, weakCompareAndSet may fail spuriously) ↗ · Java SE 21 API: LongAdder (contention, sum() is not an atomic snapshot) ↗ · Java SE 21 API: AtomicStampedReference ↗ · JDK 21 tool reference: jfr (print, summary, view) ↗ · JDK 21 tool reference: jcmd (Thread.print, JFR.start) ↗ · Report a problem