Skip to content

Chapter 18Part VI · Modern Java

Concurrency: Threads → Virtual Threads

6 min read5 runnable examples

In this chapter: why shared data breaks under concurrency, the toolkit Java 5 introduced, Java 8's CompletableFuture, and the Java 21 change that made blocking code scale again: virtual threads. Then Java 25's scoped values, and where structured concurrency stands in Java 27.

Key points

  • Two threads updating the same variable can lose updates. Fix it with synchronized, locks, or atomic classes such as AtomicInteger (Java 5).
  • Don't manage threads by hand: use an ExecutorService (Java 5). Combine async results with CompletableFuture (Java 8).
  • Virtual threads (Java 21) are cheap threads managed by the JVM. Millions can block on I/O at once. Use one per task: Executors.newVirtualThreadPerTaskExecutor().
  • Scoped values (Java 25) share per-call context safely; they're a better ThreadLocal.
  • Structured concurrency treats related tasks as one unit. It's still a preview in Java 27.

The problem: shared, mutable state

Picture it: two rangers, one tally board

Two rangers count hikers at the same gate using one whiteboard. Both glance at it and see 41. Both add one in their heads and write 42. Two hikers went through, but the count went up by one. The same thing happens to a counter shared by two threads.

code/ch18/SafeCounting.java
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class SafeCounting {
    static int unsafe = 0;  // shown only to explain the race
    static int locked = 0;
    static final Object LOCK = new Object();
    static final AtomicInteger atomic = new AtomicInteger(); // Java 5

    public static void main(String[] args) throws InterruptedException {
        List<Thread> threads = new ArrayList<>();
        for (int t = 0; t < 4; t++) {
            Thread th = new Thread(() -> {
                for (int i = 0; i < 100_000; i++) {
                    // read, add, write: not atomic
                    unsafe++;
                    // one thread at a time
                    synchronized (LOCK) { locked++; }
                    // a single atomic CPU operation
                    atomic.incrementAndGet();
                }
            });
            threads.add(th);
            th.start();
        }
        for (Thread th : threads) th.join();  // wait for all four
        System.out.println("synchronized: " + locked);
        System.out.println("atomic:       " + atomic.get());
        // usually less: lost updates
        System.out.println("unsafe <= 400000: " + (unsafe <= 400_000));
    }
}
synchronized: 400000
atomic:       400000
unsafe <= 400000: true

unsafe++ looks like one step but is three: read, add, write. Two threads can read the same value, both add one, and both write it back, so one increment disappears. The unsafe count usually ends up below 400,000, and differs on every run, which is what makes these bugs so hard to reproduce.

  • synchronized lets one thread at a time into a block guarded by a lock object.
  • Atomic classes (AtomicInteger, AtomicLong, LongAdder from Java 8) use CPU instructions that read-modify-write in one indivisible step.
  • Immutable objects (records, List.of, java.time) can't be corrupted at all, which is why modern Java leans on them.

Java 5: executors instead of threads

Creating a new Thread for every task is expensive and unbounded. Java 5's java.util.concurrent (JSR 166, led by Doug Lea) separated what to run from which thread runs it:

Picture it: a kitchen with a ticket rail

A restaurant doesn't hire a new cook for every order. Orders go on a ticket rail, and a fixed team of cooks takes the next ticket whenever one of them is free. An ExecutorService is that kitchen: you submit tasks (tickets), a pool of threads (the cooks) runs them, and a Future is the receipt you trade in for the dish.

code/ch18/ExecutorsDemo.java
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ExecutorsDemo {
    public static void main(String[] args) throws Exception {
        ExecutorService pool = Executors.newFixedThreadPool(4);  // Java 5
        try {
            List<Callable<Integer>> jobs = new ArrayList<>();
            for (int chapter = 1; chapter <= 20; chapter++) {
                final int c = chapter;
                jobs.add(() -> c * 10);  // pretend: pages in chapter c
            }
            int total = 0;
            for (Future<Integer> f : pool.invokeAll(jobs)) total += f.get();
            System.out.println("total pages: " + total);
        } finally {
            pool.shutdown();
        }
    }
}
total pages: 2100

The package also brought ConcurrentHashMap, CountDownLatch, Semaphore, BlockingQueue and explicit Locks, and Java 7 added the Fork/Join framework for divide-and-conquer work (it powers parallel streams).

Java 8: CompletableFuture

A Future can only be waited on. CompletableFuture can be composed:

code/ch18/CompletableDemo.java
import java.util.concurrent.CompletableFuture;

public class CompletableDemo {
    // pretend these call remote services
    static int fetchPrice()    { return 4500; }
    static double fetchTaxRate() { return 0.10; }

    public static void main(String[] args) {
        // Java 8
        CompletableFuture<Integer> price =
                CompletableFuture.supplyAsync(CompletableDemo::fetchPrice);
        CompletableFuture<Double> tax =
                CompletableFuture.supplyAsync(CompletableDemo::fetchTaxRate);

        String total = price
                .thenCombine(tax, (p, r) -> p + Math.round(p * r))
                .thenApply(c -> String.format("$%d.%02d", c / 100, c % 100))
                .exceptionally(e -> "unavailable")
                .join();
        System.out.println(total);
    }
}
$49.50

Two calls run concurrently, thenCombine merges their results, thenApply formats, and exceptionally supplies a fallback. It works, but long chains get hard to read and debug, because stack traces no longer show where a task came from. That pain is what the next two features address.

Java 21: virtual threads

A classic Java thread is a platform thread: a thin wrapper around an operating-system thread, which costs about a megabyte of stack and takes real time to create. Servers that gave each request its own thread ran out of threads long before they ran out of CPU, because most threads were just waiting for a database or an HTTP call. The workarounds were thread pools and asynchronous, callback-style code.

Virtual threads (JEP 444, Project Loom) are threads managed by the JVM. When one blocks on I/O, the JVM parks it and runs another virtual thread on the same underlying carrier thread.

Picture it: waiters and the kitchen

A platform thread is a waiter who takes an order and then stands at the table until the kitchen is done. With 200 waiters you can serve 200 tables, however idle those waiters are.

With virtual threads, the order becomes a ticket (the virtual thread), and the waiter (a carrier thread) hands it in and moves on. While the dish cooks, the waiter serves other tables. When the food is ready, whichever waiter is free picks the ticket up again. A few waiters can now look after thousands of tables, as long as most of the time is spent waiting for the kitchen.

flowchart TB
    subgraph JVM["JVM"]
        V1["virtual thread 1<br/>(waiting on DB)"]
        V2["virtual thread 2<br/>(running)"]
        V3["virtual thread 3 … 10,000<br/>(parked)"]
    end
    subgraph OS["Operating system"]
        C1["carrier thread"]
        C2["carrier thread"]
    end
    V2 --> C1
    V1 -. "unmounted while blocked" .- C2
code/ch18/VirtualThreads.java
import java.time.Duration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class VirtualThreads {
    public static void main(String[] args) {
        AtomicInteger done = new AtomicInteger();
        // Java 21
        try (ExecutorService exec =
                     Executors.newVirtualThreadPerTaskExecutor()) {
            for (int i = 0; i < 10_000; i++) {
                exec.submit(() -> {
                    // blocking is cheap on a virtual thread
                    Thread.sleep(Duration.ofMillis(50));
                    done.incrementAndGet();
                    return null;
                });
            }
        }  // close() waits for every task
        System.out.println(done.get() + " tasks finished");

        Thread vt = Thread.ofVirtual().unstarted(() -> {});
        System.out.println("virtual? " + vt.isVirtual());
    }
}
10000 tasks finished
virtual? true

Ten thousand tasks, each sleeping for 50 ms, finish in roughly the time of one, without a pool to size. Notice the code is ordinary, blocking, readable Java: no callbacks.

  • Use one virtual thread per task, and don't pool them; they're cheap.
  • They help with waiting (I/O), not computing. CPU-heavy work still needs about as many threads as cores.
  • ExecutorService became AutoCloseable in Java 19, so try-with-resources waits for all tasks.
  • In Java 21, a virtual thread blocking inside synchronized could "pin" its carrier thread. Java 24 fixed this (JEP 491), so synchronized is fine again.

Java 25: scoped values

ThreadLocal has long been the way to pass context, such as the current user or a request ID, without adding a parameter to every method. But thread-locals are mutable, can leak if not removed, and are costly to copy into millions of virtual threads. Scoped values (JEP 506), final in Java 25, bind a value for the duration of a call:

Picture it: a visitor badge

A ThreadLocal is like a sticky note left on a desk that many people take turns at (a pooled thread). Anyone can change it, and if you forget to remove it, the next person finds it. A scoped value is a visitor badge: handed out at the entrance, read-only, valid only inside the building, and collected automatically on the way out.

code/ch18/ScopedValues.java
public class ScopedValues {
    // Java 25 (preview in 21-24)
    static final ScopedValue<String> USER = ScopedValue.newInstance();

    static void audit(String action) {
        String who = USER.isBound() ? USER.get() : "anonymous";
        System.out.println(who + " " + action);
    }

    public static void main(String[] args) {
        ScopedValue.where(USER, "asha").run(() -> {
            audit("opened chapter 18");
            audit("ran the samples");
        });
        audit("left");  // outside the scope: not bound
    }
}
asha opened chapter 18
asha ran the samples
anonymous left

The binding is immutable, visible to everything called inside run, and automatically gone afterwards, so there's nothing to clean up.

Structured concurrency (preview)

Picture it: a group hike

A group leaves the trailhead together, and the leader doesn't call the hike finished until everyone is back. If one hiker is hurt, the leader turns the whole group around.

The last piece of Project Loom works the same way. It treats a group of concurrent subtasks like a block of code: they start together, finish together, and if one fails, the others are cancelled. It has been in preview since Java 21 and is in its seventh preview in Java 27 (JEP 533), so the API may still change. Its shape as of the Java 25 preview looks like this:

// Preview API: needs --enable-preview,
// and details may change before it is final.
Response handle(String id) throws InterruptedException {
    try (var scope = StructuredTaskScope.open()) {
        Subtask<User> user   = scope.fork(() -> findUser(id));
        Subtask<Order> order = scope.fork(() -> fetchOrder(id));
        scope.join();   // waits for both; fails fast if either fails
        return new Response(user.get(), order.get());
    }
}

This book doesn't run a test for it, because its API has changed between previews. Check the JEP for your JDK version before trying it.

Migration tip

For a typical request-per-thread server on Java 21+, switching the request executor to virtual threads is often a one-line change. Before you do, remove thread pools that exist only to limit concurrency (use a Semaphore instead), and check that libraries don't cache expensive objects in ThreadLocals.

Go deeper in the complete edition

This page is the focused edition. The complete edition of this chapter on GitHub adds the deep dives, puzzles, exercises and interview questions, with every sample ready to run.

Read the complete chapter →

Summary

  • Shared mutable state needs synchronized, atomics, or (better) immutability.
  • Executors (5) and CompletableFuture (8) replaced hand-managed threads.
  • Virtual threads (21) make blocking code scale; scoped values (25) carry context safely.
  • Structured concurrency is still in preview in Java 27.

Next: Chapter 19 brings records, sealed types and pattern matching together into a style called data-oriented programming.