JAVA-240 · Lesson 3 of 12 · 23 min

Race conditions and synchronization

▶ Video coming soon A video version of this lesson is on the way.

On Monday, January 24, 2028, Kofi brings a printout to the back room. On a test run the week before, two copies of an early OrderDesk prototype had each sold the last inner tube of one size, and the stock count had ended at minus one. "The shelf had one tube," he says. "Two customers got a confirmation. One of them is going to be unhappy." Ingrid smiles, because this is exactly the bug tonight's lesson is about. When threads share data and at least one of them changes it, the program needs rules about who may touch the data and when. Without them, the result depends on timing, and timing is never the same twice.

A counter that loses updates

Start with the smallest possible shared data: a single counter. Four threads each add 1 to it 100,000 times, so the final value should be 400,000. The program below does exactly that, with no protection at all. It is shown here as a listing rather than an example with fixed output, because its output changes from run to run, as the transcript of three real runs shows.

Java Example · Main.java
public class Main {
    static int sold = 0;

    public static void main(String[] args) throws InterruptedException {
        Thread[] clerks = new Thread[4];
        for (int i = 0; i < 4; i++) {
            clerks[i] = new Thread(() -> {
                for (int n = 0; n < 100_000; n++) sold++;
            });
            clerks[i].start();
        }
        for (Thread t : clerks) t.join();
        System.out.println("expected 400000, got " + sold);
    }
}

What this code does

Four threads each increment the same static counter 100,000 times, and main joins all four before printing. There is no synchronization at all, so increments made at the same moment can overwrite each other. The total printed is usually less than 400,000, and by a different amount each run.

Three runs
$ javac Main.java
$ java Main
expected 400000, got 306660
$ java Main
expected 400000, got 323654
$ java Main
expected 400000, got 142020

What this code does

These are three real runs of the program above on the same computer. The numbers differ from run to run, which is exactly the point: the output of a racy program cannot be predicted.

The program is not wrong because of join: main does wait for every thread. It is wrong because sold++ is not one step. The processor reads the current value of sold into a register, adds 1, and writes the result back. If two threads read the value 1,000 at the same moment, both add 1 and both write 1,001. Two sales happened, but the counter went up by one. That lost write is called a lost update, and the general problem is a race condition: the result depends on how the threads' steps happen to interleave.

Two columns of steps for threads A and B over time. A reads sold as 1000, B reads sold as 1000, A adds one and writes 1001, B adds one and writes 1001. A red note at the bottom says two increments happened but sold went up by one.
A lost update: read, add, write, interleaved

Race conditions are hard to find because they often do not show up. On a quiet weekday, with one order every few minutes, two threads almost never touch the counter in the same microsecond, and the program seems fine for months. On the first busy Saturday the bug appears, and it may disappear again as soon as someone adds a print statement to investigate, because the print changes the timing. Programmers call such bugs heisenbugs.

Check-then-act

Kofi's minus-one tube is a second kind of race, called check-then-act. The code checks a condition, then acts on it, assuming the condition is still true. Between the check and the action another thread can change the data:

Java Example · The racy reservation
static int tubesLeft = 1;

static boolean reserveTube() {
    if (tubesLeft > 0) {      // check
        tubesLeft--;          // act
        return true;
    }
    return false;
}

What this code does

Each call checks that a tube is left and then takes it. If two threads both pass the check before either has taken the tube, both take it and the count drops to -1. The check and the action must happen as one indivisible step for the method to be correct.

Other common check-then-act patterns include creating an object only if it does not exist yet (lazy initialization), adding a key to a map only if it is missing, and booking a repair slot only if it is free. Each is correct with one thread and broken with two.

Critical sections and atomicity

The code that reads and writes the shared data, sold++ or the check-and-take in reserveTube, is called a critical section. To be correct, a critical section must be atomic with respect to the other threads: either a thread runs the whole section before another thread can see or change the data, or it has not started it yet. Nothing in between can be visible. The simplest tool Java offers for making a critical section atomic is the synchronized keyword.

The synchronized keyword

Every Java object has a built-in lock, called its monitor or intrinsic lock. A synchronized block names an object and runs its body only while holding that object's monitor. If another thread already holds it, the newcomer waits, in the BLOCKED state from lesson 2, until the holder leaves its block. Only one thread can hold a given monitor at a time, so two synchronized blocks on the same object can never run at once. This property is called mutual exclusion.

Java Example · Main.java
public class Main {
    static int sold = 0;
    static final Object counterLock = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread[] clerks = new Thread[4];
        for (int i = 0; i < 4; i++) {
            clerks[i] = new Thread(() -> {
                for (int n = 0; n < 100_000; n++) {
                    synchronized (counterLock) {
                        sold++;
                    }
                }
            });
            clerks[i].start();
        }
        for (Thread t : clerks) t.join();
        System.out.println("expected 400000, got " + sold);
    }
}
Output
expected 400000, got 400000

What this code does

This is the losing counter with one change: each increment happens inside a synchronized block on the same lock object. Only one thread at a time can be inside the block, so no increment can overwrite another. The total is exactly 400,000 on every run.

A synchronized method is shorthand for a synchronized block around the whole method body. An instance method that is synchronized locks the object it is called on, this; a static method that is synchronized locks the Class object. The fixed reservation method below locks this, so all calls on the same Shelf object take turns.

Java Example · Main.java
public class Main {
    static class Shelf {
        private int tubesLeft = 1;
        private int confirmations = 0;

        synchronized boolean reserveTube() {
            if (tubesLeft > 0) {
                tubesLeft--;
                confirmations++;
                return true;
            }
            return false;
        }

        synchronized int tubesLeft() { return tubesLeft; }
        synchronized int confirmations() { return confirmations; }
    }

    public static void main(String[] args) throws InterruptedException {
        Shelf shelf = new Shelf();
        Thread[] customers = new Thread[8];
        for (int i = 0; i < 8; i++) {
            customers[i] = new Thread(shelf::reserveTube);
            customers[i].start();
        }
        for (Thread t : customers) t.join();
        System.out.println("confirmations sent: " + shelf.confirmations());
        System.out.println("tubes left: " + shelf.tubesLeft());
    }
}
Output
confirmations sent: 1
tubes left: 0

What this code does

Eight customer threads try to reserve the single tube at once, and reserveTube is synchronized, so the check and the take happen as one step. Exactly one customer gets a confirmation and the count ends at 0, never -1. Even the methods that only read the counts are synchronized, for a reason the next sections explain.

Two rules keep synchronized code correct. First, every access to a piece of shared data, reads as well as writes, must be guarded by the same lock. Synchronizing the writes but not the reads leaves the readers free to see half-finished work. Second, keep critical sections short. While one thread holds a lock, every other thread that needs it waits, so a lock held during a one-second supplier call turns a concurrent program back into a sequential one.

Java's monitors are reentrant: a thread that already holds a lock can enter another block synchronized on the same object without waiting for itself. A synchronized method can therefore call another synchronized method on the same object safely.

Visibility: seeing other threads' writes

Atomicity is only half of the story. The other half is visibility: when one thread writes a variable, when is another thread guaranteed to see the new value? The surprising answer is that, without synchronization, it may never be. Processors keep copies of data in their caches, and both the compiler and the processor are allowed to reorder or optimize reads and writes as long as a single thread cannot tell the difference. A thread spinning in a loop on an ordinary boolean field may read it once, keep the value in a register and never notice that another thread has changed it.

The rules that say when writes become visible are called the Java Memory Model. It is defined in terms of a relationship called happens-before: if one action happens-before another, the first one's effects are visible to the second. The most useful happens-before rules are:

  • Everything a thread does before it releases a lock happens-before everything another thread does after it acquires the same lock.
  • A write to a volatile field happens-before every later read of that field.
  • Everything done before Thread.start happens-before the started thread's first action.
  • Everything a thread does happens-before another thread returns from join on it.

The last two rules are why the examples in lessons 1 and 2 worked without any locks: each thread wrote its own slot, and main read the slots only after join, so the join made every write visible. The synchronized reads in the Shelf class rely on the first rule.

volatile for flags

A field declared volatile is always read from and written to main memory, in effect, and its writes and reads follow the happens-before rule above. volatile is the right tool for a simple flag that one thread sets and others read, such as a request to stop.

Java Example · Main.java
public class Main {
    static volatile boolean closing = false;

    public static void main(String[] args) throws InterruptedException {
        long[] passes = new long[1];
        Thread printer = new Thread(() -> {
            while (!closing) {
                passes[0]++;
            }
        });

        printer.start();
        Thread.sleep(50);
        closing = true;
        printer.join();

        System.out.println("printer stopped after closing was set: true");
        System.out.println("printer ran at least once: " + (passes[0] > 0));
    }
}
Output
printer stopped after closing was set: true
printer ran at least once: true

What this code does

A worker thread loops until the volatile flag closing becomes true, and main sets the flag after 50 milliseconds and joins the worker. Because the field is volatile, the worker is guaranteed to see the change and leave its loop, so join returns. The number of passes varies, so the program prints only facts that are the same on every run.

Without volatile, that program might hang forever, because the JIT compiler is allowed to hoist the read of closing out of the loop. Whether it does depends on the JVM, the processor and how long the loop has run, which is exactly the kind of behavior that makes visibility bugs miserable to track down.

volatile does not make compound actions atomic. A volatile int counter still loses updates with count++, because the read and the write are still two steps. Use volatile for single writes and reads of independent values, such as flags and references to objects that are never changed after they are published. For read-modify-write operations, use a lock or the atomic classes of the next lesson.

Three kinds of sharing problem and their fixes
ProblemExampleTool that fixes it
Lost updateTwo threads run sold++ at oncesynchronized, a lock, or an atomic variable
Check-then-actTwo threads both see one tube and both take itsynchronized around the check and the act
VisibilityA worker never sees the stop flag changevolatile, or reading and writing under the same lock
Half-built objectAnother thread sees an object before its constructor finishedPublish through a volatile field, a lock or a thread-safe collection

Safe sharing without locks

The safest shared data is data nobody changes. An immutable object, whose fields are all final and set in the constructor, can be shared freely between threads once it has been constructed, because there is nothing to race on. Java records are a natural fit: a record of a part code and a price can be handed to any number of threads. The second safest strategy is confinement: keep each piece of mutable data owned by one thread only, as the slot-per-thread examples did. Locks are the tool for the cases left over, where several threads really do need to change the same data.

Ingrid gives Hamza an order of preference that he writes on the whiteboard: first, do not share; second, share only immutable data; third, use a thread-safe class someone else has already tested, such as those in lessons 4 and 5; and only last, write your own synchronized code.

Synchronizing a whole operation

A lock must cover every step that has to happen together, not each step separately. The ledger below moves money between two cash drawers. Synchronizing each drawer's methods separately would not be enough: another thread could look at the totals between the withdrawal and the deposit and see money missing. The whole transfer must be one critical section.

Java Example · Main.java
public class Main {
    static class Ledger {
        private int counterDrawer = 50_000;
        private int safe = 150_000;

        synchronized void moveToSafe(int cents) {
            counterDrawer -= cents;
            safe += cents;
        }

        synchronized int total() {
            return counterDrawer + safe;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Ledger ledger = new Ledger();
        boolean[] mismatch = {false};

        Thread mover = new Thread(() -> {
            for (int i = 0; i < 100_000; i++) ledger.moveToSafe(1);
        });
        Thread auditor = new Thread(() -> {
            for (int i = 0; i < 100_000; i++) {
                if (ledger.total() != 200_000) mismatch[0] = true;
            }
        });

        mover.start();
        auditor.start();
        mover.join();
        auditor.join();

        System.out.println("auditor ever saw a wrong total: " + mismatch[0]);
        System.out.println("final total: " + ledger.total());
    }
}
Output
auditor ever saw a wrong total: false
final total: 200000

What this code does

One thread moves a cent at a time from the counter drawer to the safe, and an auditor thread keeps checking that the two add up to 200,000 cents. Because the transfer and the total are both synchronized on the same ledger, the auditor can never see the moment between the withdrawal and the deposit. It never sees a wrong total, and the final total is unchanged.

Try it yourself

A synchronized tally

The Tally class counts brake pads sold. Start three threads that each call sell() 50,000 times, and make the class thread-safe with synchronized methods so the program always prints pads sold: 150000.

public class Main {
    static class Tally {
        private int sold = 0;
        void sell() { sold++; }
        int sold() { return sold; }
    }

    public static void main(String[] args) throws InterruptedException {
        Tally tally = new Tally();
        Thread[] ts = new Thread[3];
        // create and start three threads that each call tally.sell() 50,000 times
        for (Thread t : ts) if (t != null) t.join();
        System.out.println("pads sold: " + tally.sold());
    }
}

Try it yourself

Book the last slot once

Tamsin has 2 repair slots left on Saturday. Start six customer threads that call book() at once, and make book() safe so that exactly two bookings succeed. The program should print bookings: 2 and slots left: 0.

public class Main {
    static class Saturday {
        private int slotsLeft = 2;
        private int bookings = 0;
        boolean book() {
            if (slotsLeft > 0) {
                Thread.yield();
                slotsLeft--;
                bookings++;
                return true;
            }
            return false;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Saturday sat = new Saturday();
        Thread[] ts = new Thread[6];
        // start six customer threads that each call sat.book()
        for (Thread t : ts) if (t != null) t.join();
        System.out.println("bookings: " + sat.bookings);
        System.out.println("slots left: " + sat.slotsLeft);
    }
}

Common mistakes

  • MistakeAssuming count++ is a single step.

    InsteadIt is a read, an add and a write; protect it with a lock or an atomic variable.

  • MistakeSynchronizing writes but not reads.

    InsteadGuard every access to shared data, reads included, with the same lock.

  • MistakeUsing different lock objects for the same data.

    InsteadOne piece of shared data needs one lock that every access uses.

  • MistakeHolding a lock during slow work such as a network call.

    InsteadDo the slow work outside the critical section and lock only to update shared data.

  • MistakeUsing volatile for a counter.

    Insteadvolatile gives visibility, not atomicity; use AtomicInteger or a lock for counters.

Summary and key terms

  • A race condition happens when the result depends on how threads' steps interleave. count++ is a read, an add and a write, so concurrent increments lose updates.
  • Check-then-act code fails when another thread changes the data between the check and the action.
  • A critical section must be atomic: no other thread may see it half done. synchronized blocks and methods use each object's monitor lock to give mutual exclusion, and monitors are reentrant.
  • Every access to shared data, reads included, must use the same lock, and critical sections should be short.
  • The Java Memory Model defines visibility through happens-before: releasing a lock, writing a volatile field, starting a thread and finishing a thread that is joined all make earlier writes visible. volatile suits flags but does not make compound actions atomic.
  • The safest designs avoid sharing mutable data: confine it to one thread or make it immutable.
Lost update
A change that is overwritten because two threads read and wrote the same value at the same time.
Check-then-act
A pattern that checks a condition and then acts, which races if the condition can change in between.
Critical section
Code that reads or writes shared data and must not interleave with other threads doing the same.
Atomic
Happening as one indivisible step as far as other threads can tell.
Monitor
The built-in lock every Java object has, used by synchronized.
Mutual exclusion
The guarantee that only one thread at a time runs a critical section.
Reentrant lock
A lock that a thread already holding it can acquire again without waiting.
Visibility
Whether one thread is guaranteed to see the values another thread wrote.
Java Memory Model
The rules that define when writes by one thread are visible to another.
happens-before
The relationship that guarantees one action's effects are visible to another.
volatile
A field modifier that guarantees visibility and ordering for single reads and writes.
Immutable object
An object whose state cannot change after construction, safe to share between threads.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. Why can the unsynchronized counter print a different total each run?

    Show answer

    Increments that interleave overwrite each other, and how often that happens depends on timing.

  2. Which object does a synchronized instance method lock?

    Show answer

    this, the object the method is called on.

  3. Is a volatile int safe for count++ from several threads?

    Show answer

    No: volatile gives visibility but count++ is still three steps.

  4. Why did the lesson 1 slot-per-thread sum need no locks?

    Show answer

    No two threads shared a slot, and main read the slots only after join.

  5. What is wrong with synchronizing moveToSafe but not total?

    Show answer

    The auditor could read the drawers between the withdrawal and the deposit and see money missing.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz