Skip to content
notes.vinhnq.com

Search

ConcurrencyMid18 min read

Java concurrency: race conditions, volatile and deadlocks

Review 11 cards
On this page

Threads and concurrency in Java series: 1. Creating threads and the lifecycle · 2. Race conditions, volatile, deadlocks · 3. ExecutorService and virtual threads

When threads share data, three bugs show up: a counter drifts, one thread never sees another thread’s change, and two threads wait for each other forever. After this post you can fix all three and find a deadlock in a thread dump. Read part 1 first, especially the BLOCKED state and join(); every demo was run on JDK 21.

1. Race conditions: several threads updating one variable

Key idea: count++ isn’t one step but a read, an add and a write. When two threads interleave, increments get lost; fix it with a lock, CAS or a striped counter.

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAdder;

public class RaceCondition {
    static final int THREADS = 4;
    static final int PER_THREAD = 1_000_000;

    static int unsafeCount = 0;

    static int syncCount = 0;
    static synchronized void syncIncrement() { syncCount++; }

    static final AtomicInteger atomicCount = new AtomicInteger();
    static final LongAdder adder = new LongAdder();

    static void runInParallel(Runnable increment) throws InterruptedException {
        List<Thread> threads = new ArrayList<>();
        for (int i = 0; i < THREADS; i++) {
            Thread t = new Thread(() -> {
                for (int j = 0; j < PER_THREAD; j++) increment.run();
            });
            threads.add(t);
            t.start();
        }
        for (Thread t : threads) t.join();
    }

    public static void main(String[] args) throws InterruptedException {
        runInParallel(() -> unsafeCount++);
        runInParallel(RaceCondition::syncIncrement);
        runInParallel(atomicCount::incrementAndGet);
        runInParallel(adder::increment);

        System.out.println("expected     = " + THREADS * PER_THREAD);
        System.out.println("unsafe       = " + unsafeCount);
        System.out.println("synchronized = " + syncCount);
        System.out.println("atomic       = " + atomicCount.get());
        System.out.println("LongAdder    = " + adder.sum());
    }
}
// expected     = 4000000
// unsafe       = 1252965   (different on every run)
// synchronized = 4000000
// atomic       = 4000000
// LongAdder    = 4000000

Almost 70% of the increments vanished because two threads both read 10, both compute 11, both write 11. A race condition is when the result depends on how the threads happen to interleave. (The Java basics post mentions that i++ isn’t atomic; this is the full demo.)

Two shapes of race you’ll see all the time:

  • Read-modify-write: count++, balance -= amount, map.put(k, map.get(k) + 1).
  • Check-then-act: if (stock > 0) stock--, if (!map.containsKey(k)) map.put(k, v), “check that the username is free, then insert”. Between the check and the act, another thread has changed the state.

Races aren’t limited to one JVM. A cache and a database share no transaction, so a writer deleting a cache key while a slow reader refills it with the old value is also check-then-act. Cache-aside and keeping the cache consistent with the database walks through each of those interleavings and how to close the gap (delete after commit, TTL, versions, leases).

The three fixes in the demo:

  • synchronized: every object has a monitor (a hidden lock attached to the object). Only one thread at a time can be inside a synchronized block on the same monitor; the others go BLOCKED. A static synchronized method locks on RaceCondition.class, an instance method on this. The lock is reentrant: the holder can enter again.
  • AtomicInteger: uses the CPU’s compare-and-swap (CAS) instruction: “if the value is still 10, write 11; otherwise report failure so I can retry”. Nobody blocks.
  • LongAdder: splits the counter into cells, each thread adds to its own cell, and reading sums them. Fastest when many threads write (metrics, rate counters), but sum() isn’t an atomic snapshot.
LongAdder, @Contended and false sharing

LongAdder’s cells are marked @Contended so they don’t share a cache line (the 64-byte block a CPU loads at once). If two cells shared a line, every write by one core would force the other core to reload the whole block: that’s false sharing, two unrelated variables slowing each other down several times over. The numbers are in Virtual memory, page cache and the JVM. The LMAX Disruptor pads its sequences onto separate cache lines for the same reason; see A liveness system: a timing wheel and the LMAX Disruptor.

2. Visibility, volatile and happens-before

Key idea: without synchronization, one thread can write and the other may never see it. volatile (or a lock) creates a happens-before relationship that guarantees visibility.

public class Visibility {
    static boolean running = true;           // try adding volatile

    public static void main(String[] args) throws InterruptedException {
        Thread worker = new Thread(() -> {
            long spins = 0;
            while (running) {
                spins++;                         // no synchronization, no I/O
            }
            System.out.println("stopped after " + spins + " spins");
        });
        worker.start();
        Thread.sleep(1000);                      // give the JIT time to compile the loop
        running = false;
        System.out.println("main set running = false");
        worker.join(3000);
        System.out.println("worker alive after 3s? " + worker.isAlive());
        System.exit(0);
    }
}
// Without volatile: main set running = false
//                   worker alive after 3s? true      <- spins forever
// With volatile:    main set running = false
//                   stopped after 3673716389 spins
//                   worker alive after 3s? false

Without volatile, the JIT (the runtime compiler) may assume running never changes inside the loop and hoist the read out, turning the loop into while (true). CPUs also have per-core caches and store buffers. The Java Memory Model (JMM) only guarantees that one thread sees another’s write if there’s a happens-before relationship between the two actions (“A happens-before B” meaning everything A wrote is visible to B).

The happens-before rules you’ll use most:

Rule Meaning
Program order Within one thread, an earlier statement happens-before a later one
Monitor Unlocking a monitor happens-before every later lock of the same monitor
volatile A write to a volatile variable happens-before every later read of it
Thread.start() Everything before t.start() is visible inside thread t
Thread.join() Everything thread t did is visible after t.join() returns
Executors, Future Submitting a task happens-before it runs; the task finishing happens-before future.get() returns
Transitivity If A hb B and B hb C, then A hb C

Thanks to transitivity, volatile also “carries” ordinary variables: thread A writes config = ... and then the volatile ready = true; if thread B sees ready == true, it’s guaranteed to see the new config. That’s how java.util.concurrent classes publish data safely.

3. Deadlocks

Key idea: a deadlock is threads holding locks and waiting for each other’s locks in a cycle, with nobody letting go. Breaking one of the four Coffman conditions prevents it, usually by locking in a fixed order.

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

public class Deadlock {
    static final Object ORDERS = new Object();
    static final Object INVENTORY = new Object();

    static void pause() {
        try { Thread.sleep(50); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread checkout = new Thread(() -> {
            synchronized (ORDERS) {
                pause();
                synchronized (INVENTORY) { System.out.println("checkout done"); }
            }
        }, "checkout");
        Thread restock = new Thread(() -> {
            synchronized (INVENTORY) {
                pause();
                synchronized (ORDERS) { System.out.println("restock done"); }
            }
        }, "restock");
        checkout.setDaemon(true);   // so the demo can exit even though both threads are stuck
        restock.setDaemon(true);
        checkout.start();
        restock.start();
        Thread.sleep(500);

        ThreadMXBean mx = ManagementFactory.getThreadMXBean();
        long[] ids = mx.findDeadlockedThreads();
        if (ids != null) {
            for (ThreadInfo info : mx.getThreadInfo(ids)) {
                System.out.println(info.getThreadName() + " waits for " + info.getLockName()
                        + " held by " + info.getLockOwnerName());
            }
        }
    }
}
// checkout waits for java.lang.Object@78b66d36 held by restock
// restock waits for java.lang.Object@6f96c77 held by checkout
flowchart LR
  C[Thread checkout] -- holds --> O((ORDERS))
  R[Thread restock] -- holds --> I((INVENTORY))
  C -. waits for .-> I
  R -. waits for .-> O

The four Coffman conditions; remove any one and there’s no deadlock:

  • Mutual exclusion: a lock has a single owner.
  • Hold and wait: holding one lock while waiting for another.
  • No preemption: nobody can take a lock away from another thread.
  • Circular wait: A waits for B, B waits for A.

Detecting it

Key idea: a thread dump (jstack, jcmd Thread.print) prints “Found one Java-level deadlock” with the threads, the locks and the lines of code.

  • jstack <pid> or jcmd <pid> Thread.print: shows which thread holds and waits for which lock.
  • In code: ThreadMXBean.findDeadlockedThreads() (as in the demo), which you can hook into a health check or a monitoring job.
  • Symptoms in the wild: CPU near zero while requests hang, and the number of BLOCKED threads keeps growing. Take two or three thread dumps a few seconds apart to tell a deadlock (nothing moves) from heavy lock contention (slow progress).

Database deadlocks (two transactions locking rows in opposite order) are detected by the database, which aborts one transaction; prevention is identical: update rows in a fixed order.

Thread dump of the deadlock demo
Found one Java-level deadlock:
=============================
"checkout":
  waiting to lock monitor 0x00000076a36b50a0 (object 0x0000007010375760, a java.lang.Object),
  which is held by "restock"
"restock":
  waiting to lock monitor 0x00000076a36b5180 (object 0x0000007010375750, a java.lang.Object),
  which is held by "checkout"
...
"checkout":
	at Deadlock.lambda$main$0(Deadlock.java:17)
	- waiting to lock <0x0000007010375760> (a java.lang.Object)
	- locked <0x0000007010375750> (a java.lang.Object)

Preventing it

Key idea: take locks in one global order (breaks “circular wait”), or use tryLock with a timeout, then release everything and retry (breaks “hold and wait”).

// Option 1: always lock in one global order (here: ascending id)
static void transferOrdered(Account from, Account to, long amount) {
    Account first = from.id < to.id ? from : to;
    Account second = first == from ? to : from;
    synchronized (first) {
        synchronized (second) {
            if (from.balance < amount) return;
            from.balance -= amount;
            to.balance += amount;
        }
    }
}

// Option 2: tryLock with a timeout; if you can't get both, release everything and retry
static boolean transferTryLock(Account from, Account to, long amount) throws InterruptedException {
    long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(1);
    while (System.nanoTime() < deadline) {
        if (from.lock.tryLock(10, TimeUnit.MILLISECONDS)) {
            try {
                if (to.lock.tryLock(10, TimeUnit.MILLISECONDS)) {
                    try {
                        if (from.balance < amount) return false;
                        from.balance -= amount;
                        to.balance += amount;
                        return true;
                    } finally {
                        to.lock.unlock();
                    }
                }
            } finally {
                from.lock.unlock();
            }
        }
        Thread.sleep(ThreadLocalRandom.current().nextInt(1, 5)); // random backoff avoids livelock
    }
    return false;
}

Both versions ran 100,000 random transfers between 5 accounts on 8 threads: no hang, and the total stayed at 5,000 (full code in Exercise 2).

The four Coffman conditions apply in exactly the same way to database locks: the post on optimistic or pessimistic locking shows how to break each one with InnoDB (lock in id order, take all locks in one statement, the deadlock detector). synchronized only locks within one JVM, so it can’t protect data shared in a database when you run several instances.

Exercises

Exercise 1: A limited-stock sale

Task: a flash sale has 100 items and 10,000 buy requests arrive at once on 16 threads. Write buy() returning true when the purchase succeeds. Never sell more than 100.

Solution to Exercise 1: a CAS loop and synchronized

The usual mistake: using an AtomicInteger but splitting get() and decrementAndGet(). Each step is atomic; the check-then-act as a whole is not:

boolean buy() {                       // WRONG
    if (left.get() > 0) {
        sleepQuietly();               // simulate a bit of work between check and act
        left.decrementAndGet();
        return true;
    }
    return false;
}

Two correct solutions:

// Lock-free: a CAS loop
boolean buy() {
    while (true) {
        int current = left.get();
        if (current == 0) return false;
        if (left.compareAndSet(current, current - 1)) return true;
    }
}

// Lock-based: simple, and easy to extend when several fields must change together
synchronized boolean buy() {
    if (left == 0) return false;
    left--;
    return true;
}

Test run (with a CountDownLatch as a “starting gun” so every thread starts at the same moment):

naive  sold=114 left=-14
cas    sold=100 left=0
locked sold=100

Senior note: when the app runs as several instances, stock has to be decremented in the database (UPDATE product SET stock = stock - 1 WHERE id = ? AND stock > 0, then check the affected row count) or with Redis DECR; a JVM lock only protects one process.

Exercise 2: Money transfers without deadlock

Task: 5 accounts with 1,000 each. Run 100,000 random transfers on 8 threads. Requirements: no deadlock, and the total is always 5,000. Write two versions: lock ordering and tryLock.

Solution to Exercise 2: a program that runs both versions
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;

public class Transfer {
    static final class Account {
        final long id;
        long balance;
        final ReentrantLock lock = new ReentrantLock();
        Account(long id, long balance) { this.id = id; this.balance = balance; }
    }

    // transferOrdered and transferTryLock: as in section 3

    public static void main(String[] args) throws Exception {
        run(false);
        run(true);
    }

    static void run(boolean useTryLock) throws Exception {
        Account[] accounts = new Account[5];
        for (int i = 0; i < accounts.length; i++) accounts[i] = new Account(i, 1_000);

        try (ExecutorService pool = Executors.newFixedThreadPool(8)) {
            for (int i = 0; i < 100_000; i++) {
                pool.submit(() -> {
                    var rnd = ThreadLocalRandom.current();
                    Account a = accounts[rnd.nextInt(accounts.length)];
                    Account b = accounts[rnd.nextInt(accounts.length)];
                    if (a == b) return null;
                    if (useTryLock) transferTryLock(a, b, rnd.nextInt(1, 50));
                    else transferOrdered(a, b, rnd.nextInt(1, 50));
                    return null;
                });
            }
        }
        long total = 0;
        for (Account a : accounts) total += a.balance;
        System.out.println((useTryLock ? "tryLock" : "ordered") + " total = " + total + " (expected 5000)");
    }
}
// ordered total = 5000 (expected 5000)
// tryLock total = 5000 (expected 5000)

Three things to notice:

  • The lambda is a Callable (it has return null) so it may throw the InterruptedException from tryLock.
  • Reading balance after the pool is closed is safe thanks to the happens-before edge of close()/awaitTermination.
  • The two versions must not be mixed on the same accounts in one run (see the pitfall about synchronized and ReentrantLock in section 1).

To see the deadlock for yourself, change transferOrdered to lock from then to without ordering: the program hangs and jstack reports “Found one Java-level deadlock”.

Remember

  • Explain why count++ loses increments, and the two shapes of race: read-modify-write and check-then-act.
  • Know when to pick synchronized, AtomicInteger or LongAdder, and when it has to move down to the database.
  • Explain why a while (running) loop hangs without volatile, and name 4–5 happens-before relationships.
  • Name the four Coffman conditions and the two ways to break them: lock ordering, tryLock + backoff.
  • Read a thread dump to tell a deadlock from heavy lock contention.

Interview questions

Junior

How does synchronized work?

Every object has a monitor; only one thread at a time can be inside a synchronized block on the same monitor.

Any other thread trying to enter is BLOCKED. An instance method locks on this, a static synchronized method on the Class object. The lock is reentrant and is released even when the block exits with an exception. Besides mutual exclusion, an unlock happens-before the next lock of the same monitor, so whatever was written inside the block is visible to the next thread.

Mid

What is a race condition, and how do you fix count++ on a shared variable?

A race condition is when the result depends on how threads interleave; fix count++ with AtomicInteger, LongAdder or a lock, not volatile.

count++ is read-add-write, so two threads that read the same value lose an increment. AtomicInteger.incrementAndGet() uses CAS; LongAdder suits many writers (e.g. metrics); synchronized/a lock when you must protect several variables or a whole check-then-act sequence.

What does volatile guarantee, and what doesn’t it?

It guarantees visibility and ordering, not atomicity.

Once written, other threads read the new value; there’s no reordering across the volatile access; a volatile write happens-before later reads. Multi-step operations like count++ are still not atomic. Use it for stop flags, publishing references to immutable objects, and double-checked locking.

Why does if (left.get() > 0) left.decrementAndGet() still oversell, even with an AtomicInteger?

Because each call is atomic, but the check-then-act pair isn’t.

Several threads see left == 1, all pass the if, then all decrement, and stock goes negative. Fix it with a CAS loop (compareAndSet(current, current - 1) only succeeds if nobody changed the value in between), with a conditional updateAndGet, or by putting both the check and the decrement inside one synchronized block.

Senior

What is a deadlock, and how do you detect and prevent it?

Threads hold locks and wait for each other’s locks in a cycle; detect it with a thread dump, prevent it with a global lock order or tryLock with a timeout.

  • It needs all four Coffman conditions: mutual exclusion, hold and wait, no preemption, circular wait.
  • Detect: jstack/jcmd Thread.print (prints “Found one Java-level deadlock”), ThreadMXBean.findDeadlockedThreads(), or the symptom of low CPU with hanging requests.
  • Prevent: a global lock order, tryLock with a timeout plus backoff, short critical sections with no I/O or foreign callbacks inside, and avoiding the need for several locks at once.
What is happens-before? Name a few important happens-before relationships.

The Java Memory Model’s rule: if A happens-before B, everything A wrote is visible to B and the two can’t be reordered past each other.

Without it, another thread may see stale values or even a half-constructed object. The main ones: program order within a thread; unlocking a monitor → a later lock of the same monitor; a volatile write → a later read; Thread.start() → code in that thread; code in a thread → join() returning; submitting a task to an executor → the task running; a task completing → Future.get() returning; and transitivity.

The app runs 3 instances. Does synchronized prevent overselling stock?

No. A JVM lock only works within one process; three pods means three unrelated monitors.

An invariant shared across instances has to be enforced where the state is shared: UPDATE ... SET stock = stock - 1 WHERE id = ? AND stock > 0 and then check the affected row count, a row lock (SELECT ... FOR UPDATE), optimistic locking with a version column, or an atomic DECR in Redis. A JVM lock can still reduce contention inside one instance, but it isn’t the final guarantee.

Useful phrases for interviews

  • “count++ looks like one operation, but it’s a read, an add and a write, so concurrent increments get lost.”
  • “volatile gives you visibility and ordering, but not atomicity; for counters I’d reach for AtomicInteger or LongAdder.”
  • “To diagnose a hang, I’d take a few thread dumps a few seconds apart; jstack even reports Java-level deadlocks for you.”
  • “We avoid deadlocks by always acquiring locks in a consistent order, for example by account id.”
  • “A check-then-act sequence has to be atomic as a whole, not just step by step.”
  • “A JVM lock only protects one process; across instances, the database has to enforce the invariant.”
  • “Every piece of shared state should be guarded by exactly one lock, and every access has to go through it.”
  • “Happens-before is what makes a write in one thread visible in another; without it, the JIT may hoist the read out of the loop.”

Notes based on the Hỏi Dân IT Java Core material plus my own research, rewritten in my own words.