Course contents

Compare-and-swap and the ABA problem

What compare-and-swap really does, the CAS retry loop with real retry counts under contention, a lock-free Treiber stack, and the ABA problem: how it breaks that stack, why Java's GC makes it rarer, and how stamps fix it.

PremiumOften askedLesson23 min readAdvancedReviewed Oct 2026 · Java 21
On this page
  1. Picture it first: "only if it still says 100"
  2. Under the hood: one instruction, three letters
  3. Lock-free vs wait-free, in plain words
  4. A lock-free stack in a dozen lines
  5. The ABA problem, told as a story first
  6. Fixing ABA: compare the history, not only the value
  7. The approach ladder: a shared stack under many threads
  8. Real world
  9. Advanced: contention, spurious failures and VarHandles
  10. How to say it in the interview

🔁 What is compare-and-swap (CAS)?

CAS is one CPU instruction that says: "set this memory slot to B, but only if it still holds A", and it does the check and the write as one indivisible step. If someone changed the slot in the meantime, nothing is written and you're told you lost. In Java you meet it as AtomicInteger.compareAndSet(expected, newValue), and it's the engine under every atomic class, ConcurrentHashMap and most of java.util.concurrent.

For this lesson, we'll write the CAS retry loop by hand and count how often it loses under real contention, build a lock-free stack on top of it, and then break that stack with the ABA problem: a bug where "it still says A" is true, and everything is wrong anyway. Interviewers love this topic because it starts as "how does AtomicInteger work without a lock?" and ends at "your lock-free stack just handed the same job to two workers. Why?" That second half is where most candidates go quiet, so it's where you can stand out.

What you’ll learn

  • Explain compare-and-swap in one sentence and name the hardware behind it (LOCK CMPXCHG on x86, LL/SC and **LSE CAS** on ARM)
  • Write the CAS retry loop with AtomicInteger.compareAndSet from memory, and predict how many attempts fail under contention
  • Tell lock-free from wait-free and from blocking, in plain words
  • Build a lock-free Treiber stack on an AtomicReference and say exactly when it stays correct
  • Walk through the ABA problem step by step, and explain why Java's GC prevents most pointer ABA while values, indexes and pooled objects still suffer it
  • Fix ABA with AtomicStampedReference, know what AtomicMarkableReference is really for, and when to reuse ConcurrentLinkedDeque instead
  • Explain why CAS under heavy contention can be slower than a lock (cache line ping-pong, wasted retries) and what backoff, getAndAdd and LongAdder change
  • Use weakCompareAndSet (spurious failure), compareAndExchange and **VarHandle** CAS on your own fields

Premium lesson

Keep reading with the Java Pass

The rest of this lesson is part of Java One-Stop Interview Prep: in-depth explanations, diagrams, model answers and how to say them in the interview.

  • Picture it first: "only if it still says 100"
  • Under the hood: one instruction, three letters
  • Lock-free vs wait-free, in plain words
  • A lock-free stack in a dozen lines
  • The ABA problem, told as a story first
  • Fixing ABA: compare the history, not only the value
  • The approach ladder: a shared stack under many threads
  • Real world
  • Advanced: contention, spurious failures and VarHandles
  • How to say it in the interview

From ₹299 · one-time payment · No auto-renewal

Practise this topicAtomics & CAS: interview questions & scenarios
Further reading (14)