Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Concurrency

    Virtual Threads (Java 21)

    A normal Java thread is a platform thread: a thin wrapper around an operating-system thread (02operatingsystems.md Ch 7), with a ~1 MB stack.

    Chapter 11 of 125 min readOften asked in interviews

    The Plain Picture#

    A restaurant where cooks are expensive (each needs a whole workstation), so there are only 200 of them. Most of each cook's time is spent waiting — for the oven, for the delivery. Virtual threads are like giving each order its own lightweight "ticket": whenever the ticket is waiting on the oven, the cook puts it aside and works on another ticket. A handful of cooks can keep thousands of orders moving.

    The Real Names#

    • A normal Java thread is a platform thread: a thin wrapper around an operating-system thread (Operating Systems Ch 7), with a ~1 MB stack. Thousands are fine; millions are not.

    • A virtual thread (Java 21) is a thread managed by the JVM, not the OS. The JVM runs many virtual threads on a small pool of platform threads called carrier threads (about one per core).

    • When a virtual thread does blocking I/O or sleeps, the JVM unmounts it from its carrier — saving its small stack to the heap — and runs another virtual thread on that carrier. When the I/O completes, it's mounted again (possibly on a different carrier).

    • Result: you write simple blocking, thread-per-request code (easy to read and debug), and get the scalability of an event loop (Operating Systems Ch 11) without callback-style code.

    Under the Hood — measured#

    Ten thousand tasks that each "wait" 100 ms (like a database or AI call):

    Java
    try (ExecutorService platform = Executors.newFixedThreadPool(200)) {
        for (int i = 0; i < 10_000; i++) platform.submit(() -> sleep(100));
    }
    
    try (ExecutorService virt = Executors.newVirtualThreadPerTaskExecutor()) {
        for (int i = 0; i < 10_000; i++) virt.submit(() -> sleep(100));
    }
    Output
    10,000 tasks x 100ms sleep: 200 platform threads 5223 ms | virtual threads 187 ms

    With 200 platform threads, the tasks run in 50 waves of 200: 50 × 100 ms ≈ 5 s. With one virtual thread per task, all 10,000 wait at the same time on 8 carrier threads: about 100 ms plus overhead. 28× faster, with simpler code.

    Rules for using virtual threads#

    1. Great for I/O-bound work — web requests, database queries, HTTP calls. No help for CPU-bound work — you still only have 8 cores; use a platform pool sized to the cores (Ch 2).

    2. Don't pool them. They're cheap to create — make one per task (newVirtualThreadPerTaskExecutor(), Thread.ofVirtual().start(...), or Thread.startVirtualThread(...)).

    3. Limit access to scarce resources with a Semaphore (Ch 9), not a small pool: 100,000 virtual threads can happily overwhelm a database that allows 100 connections (Networking 2: TCP, UDP and Ports Ch 12).

    4. Pinning: in Java 21, a virtual thread that blocks while inside a synchronized block stays stuck to its carrier ("pinned"), losing the benefit; prefer ReentrantLock around blocking calls in Java 21. (Java 24 removed this limitation for synchronized.)

    5. Be careful with ThreadLocal (Ch 12) — with millions of threads, per-thread caches multiply memory. Java's newer scoped values are the intended replacement.

    Frameworks adopted them quickly: Spring Boot can run each request on a virtual thread with a single setting. The Python equivalent idea — cheap waiting tasks — is asyncio (Python Concurrency (coming soon)).

    Check yourself

    4 questions
    1

    What's the difference between a platform thread and a virtual thread?

    Show answer

    A platform thread wraps an OS thread (big stack, expensive); a virtual thread is a lightweight JVM-managed thread run on a few carrier threads.

    2

    What happens when a virtual thread blocks on I/O?

    Show answer

    The JVM unmounts it from its carrier thread, which runs other virtual threads; it's remounted when the I/O completes.

    3

    Why did 10,000 sleeping tasks finish in ~190 ms on virtual threads but ~5,200 ms on 200 platform threads?

    Show answer

    Every task got its own virtual thread and they all waited simultaneously; the platform pool could only have 200 waiting at once (50 waves).

    4

    Should you use virtual threads for image processing? Should you pool them?

    Show answer

    No — it's CPU-bound, so use a core-sized platform pool. No — virtual threads are cheap; create one per task.