Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

Part 12 / Topic 6 of 7

Virtual threads (JEP 444)

A platform thread (every Thread so far in this part) maps to one real OS thread — expensive enough that a few thousand of them is already a lot for most machines. A virtual thread (JEP 444, finalized in Java 21) is a lightweight thread managed by the JVM itself: many virtual threads share a small pool of "carrier" platform threads, and a virtual thread that blocks (on I/O, on Thread.sleep, on a lock) gets unmounted from its carrier so the carrier can run a different virtual thread in the meantime. Think of a restaurant with a handful of tables (platform threads) and thousands of reservation slips (virtual threads) — a party that's still deciding what to order doesn't tie up the table; the host seats someone else and comes back to them.

Thread virtual = Thread.ofVirtual().name("partner-check-", 0).unstarted(() -> { ... });
// or, for many short-lived tasks:
try (ExecutorService virtualPool = Executors.newVirtualThreadPerTaskExecutor()) {
    virtualPool.submit(() -> { Thread.sleep(Duration.ofMillis(20)); ... });
}   // close() waits for every submitted task to finish
$ javac VirtualThreads.java
$ java VirtualThreads
== a virtual thread, up close ==
isVirtual: true
isDaemon: true

== 10,000 blocking "delivery partner status" checks, platform threads (capped pool) ==
completed: 10000
200 platform threads took: 1172 ms  (varies)

== the same 10,000 checks, one virtual thread per task ==
completed: 10000
10,000 virtual threads took: 76 ms  (varies)
sequential would have taken about: 200 s
virtual threads finished faster than the capped platform pool: true

10,000 tasks, each blocking for 20 ms (standing in for a slow partner app's response), would take about 200 seconds run one at a time. A capped pool of 200 platform threads gets it down to just over a second — but 200 threads is already a real OS resource cost, and that's the practical ceiling for this approach on most machines. One virtual thread per task finishes the same 10,000 blocking calls in well under a tenth of a second: creating 10,000 virtual threads costs almost nothing, and none of them occupies an OS thread while it's parked in Thread.sleep.

A virtual thread is always a daemon thread (isDaemon() is true), and isVirtual() is the one new check that tells you which kind of thread you're holding. Everything else from sections 1–5 — Runnable, join(), synchronized, even ExecutorService itself — works identically on a virtual thread; newVirtualThreadPerTaskExecutor() is the one new factory method.

⚠️ Virtual threads help with blocking I/O, not with CPU-bound work. A virtual thread doing heavy computation still occupies its carrier the whole time, same as a platform thread would — there's no magic extra CPU. The win here is specifically "blocked waiting," which is most of what a slow network call or database query actually is.

🔍 Going deeper: a synchronized block that blocks does pin a virtual thread to its carrier in Java 21 (it can't be unmounted while holding a monitor) — a known limitation, improved in later JDKs. Prefer java.util.concurrent locks and collections (section 5) over synchronized in virtual-thread-heavy code where this matters.

Full program: examples/VirtualThreads.java.

Done here? The practice set for Part 12 — including the solutions — is on the part page.

Search the guide