System Atlas
the atlas

How Java virtual threads work

Millions of threads on a handful of OS threads — concurrency without the memory bill.

the gist

Virtual threads (Project Loom, stable in Java 21) are lightweight threads managed by the JVM rather than the OS. Many virtual threads are multiplexed onto a small pool of OS 'carrier' threads; when a virtual thread blocks on I/O it unmounts, freeing its carrier to run another, so you can have millions of them cheaply. This makes the simple thread-per-request style scale to huge I/O-bound workloads that would crush traditional platform threads.

ANALOGY // A call center where a few agents handle thousands of customers: whenever a customer is put on hold (blocked), the agent instantly serves someone else, so a tiny staff keeps enormous numbers of conversations moving.
living diagram01 / 05
mountRequests1 virtual thread eachVirtual threadsmillions, JVM-managedJVM schedulermount / unmountCarrier threadssmall OS poolHeap parkstacks of blocked VTs
01

The platform-thread ceiling

A traditional Java thread is a thin wrapper over an OS thread, and each one reserves a large stack (often ~1 MB) and is scheduled by the kernel. They are expensive to create and switch, so a server can realistically hold only a few thousand. Under many concurrent connections that limit forces awkward thread pools and async code.

02

Enter virtual threads

A virtual thread is scheduled by the JVM, not the OS, and starts with a tiny stack that grows on demand. Creating one is cheap enough that you can spin up a fresh virtual thread per request or per task and have millions alive at once. To your code it looks and behaves exactly like an ordinary Thread.

03

Mounted on a carrier

To actually run, a virtual thread must be mounted onto a real OS thread called a carrier, drawn from a small ForkJoinPool. Many virtual threads share this handful of carriers — at any instant only as many run in parallel as there are carriers, but vast numbers can be in flight. This multiplexing is the core mechanism.

04

Unmount on blocking I/O

The magic happens when a virtual thread hits a blocking call — reading a socket, querying a database. Instead of freezing an OS thread, the JVM unmounts the virtual thread, copies its stack to the heap, and hands the carrier back to run someone else. The expensive OS thread is never left idle waiting on I/O.

05

Resume and win at scale

When the I/O finishes, the parked virtual thread is remounted onto any free carrier and continues exactly where it paused. Because blocked threads cost almost nothing, the simple one-thread-per-request style suddenly scales to enormous I/O-bound loads. You write plain blocking code and get async-level throughput for free.

the full picture
01

The problem virtual threads solve

For most of Java's life, a Thread was a thin wrapper around a native operating-system thread. That mapping is one-to-one: create a Java thread and the JVM asks the kernel for a real OS thread, complete with a large pre-allocated stack (commonly around 1 MB) and scheduling handled by the OS. These are now called platform threads, and they are excellent for CPU-bound work but scarce and expensive — a server can realistically manage only a few thousand before it runs out of memory or drowns in context-switching overhead.

That scarcity collides head-on with how servers actually work. A web server handles requests, and most requests spend the vast majority of their time *waiting* — for a database query, a call to another service, a file read. This is I/O-bound work. The natural, readable way to write it is one thread per request: each request gets a thread that runs top to bottom, blocking whenever it waits. But with only a few thousand platform threads available, and each blocked one uselessly pinning a precious OS thread, this simple model cannot scale to the tens of thousands of concurrent connections a busy server sees. The industry's workaround was asynchronous, non-blocking programming — callbacks, futures, reactive streams — which scales beautifully but turns straightforward code into a tangle of chained callbacks that is hard to read, debug, and reason about. Virtual threads exist to give you the scalability of async with the simplicity of blocking code.

02

What a virtual thread actually is

A virtual thread, delivered by Project Loom and made stable in Java 21, is a thread that is scheduled by the JVM rather than the operating system. It implements the exact same java.lang.Thread API, so your existing code, libraries, and mental model still apply — but underneath, it is a lightweight object living on the Java heap, not a heavyweight OS resource.

The differences are dramatic. A virtual thread starts with a tiny stack that grows and shrinks on demand rather than reserving a fixed megabyte up front, so its memory footprint is measured in hundreds of bytes to kilobytes instead of megabytes. Creating one is so cheap that virtual threads are meant to be plentiful and even disposable: rather than pooling them, you simply create a brand-new virtual thread for every task or request and let it be garbage-collected when done. The result is that an application can comfortably have millions of virtual threads alive at once, where it could only ever have thousands of platform threads. Crucially, a virtual thread is not a new kind of thread you have to program differently — it is the same abstraction made cheap.

03

Mounting, carriers, and multiplexing

Virtual threads do not run directly on the CPU; they need a real OS thread to execute on. The JVM keeps a small pool of platform threads — by default a ForkJoinPool sized to the number of CPU cores — called carrier threads. To run, a virtual thread is mounted onto a carrier; when it stops running, it unmounts. Because there are only a few carriers but potentially millions of virtual threads, the JVM is multiplexing many virtual threads onto few OS threads.

At any single instant, only as many virtual threads are truly executing as there are carriers — that is a hard limit set by the physical CPUs. What makes the scheme powerful is not more parallelism but far better *utilization*. The whole design rests on a simple observation: an I/O-bound virtual thread spends almost all its life blocked, doing nothing. So instead of letting a blocked thread hog a carrier, the JVM detects the block and swaps in a different virtual thread that has work to do. The carriers stay busy running whichever virtual threads are actually runnable, and the enormous population of blocked ones costs essentially nothing but a parked stack.

04

The unmount-on-block mechanism

The heart of virtual threads is what happens on a blocking operation. Suppose a virtual thread calls a blocking method — reading from a socket, awaiting a database response, sleeping. With a platform thread this would block the OS thread, wasting it entirely. With a virtual thread, the JVM intercepts the blocking call and does something clever instead.

It unmounts the virtual thread from its carrier: the virtual thread's current stack — its local variables and call frames — is copied out of the carrier and parked on the heap as a continuation, and the carrier is released to go run some other runnable virtual thread. No OS thread sits idle waiting on the I/O. When the I/O eventually completes, the virtual thread is marked runnable again, its saved stack is restored, and it is remounted onto whatever carrier is next free, resuming at the exact line where it paused. All of this is invisible to your code — you wrote a plain, sequential, blocking call, and the runtime transparently turned the wait into a cheap suspension. There is one caveat worth knowing: if a virtual thread blocks inside a synchronized block or a native call, it can become pinned, temporarily unable to unmount and holding its carrier — which is why modern guidance nudges hot paths toward ReentrantLock over synchronized.

05

When to use them and what they don't fix

Virtual threads are a targeted win, not a universal speed boost, and being precise about that is what separates using them well from cargo-culting them. Their sweet spot is I/O-bound concurrency: workloads with vast numbers of tasks that spend most of their time waiting — classic request handling, service-to-service calls, database-heavy backends. There, virtual threads let you keep the clean thread-per-request programming model while scaling to a level that previously demanded reactive frameworks, and they do it without you rewriting anything into callbacks. You get readable stack traces, ordinary debuggers, and simple try/finally cleanup, all of which async code sacrifices.

What they do not do is make CPU-bound work faster. If your tasks are busy computing rather than waiting, you are limited by the number of cores regardless of how many threads you spawn, and virtual threads add no magic — platform threads sized to the core count remain the right tool. They also do not remove the need to think about shared mutable state: more concurrency means the same old race conditions and the need for proper synchronization. In practice the guidance is straightforward — reach for virtual threads to handle high-concurrency I/O with simple blocking code, do not pool them (create one per task), avoid pinning them under long synchronized sections, and keep using bounded parallelism for genuine CPU work. Used this way, they quietly retire one of the longest-standing tensions in server-side Java: choosing between code that is easy to read and code that scales.

what to remember
01

A platform thread wraps one OS thread — heavy (~1 MB stack), slow to create, so you can only have thousands.

02

A virtual thread is a lightweight JVM-managed thread with a tiny, growable stack — you can have millions.

03

The JVM multiplexes many virtual threads onto a small pool of OS 'carrier' threads.

04

On a blocking I/O call, a virtual thread unmounts from its carrier and its stack is parked on the heap, freeing the carrier.

05

When the I/O completes the virtual thread is remounted on any available carrier and resumes where it left off.

06

This makes the simple, readable thread-per-request model scale for I/O-bound work — a huge concurrency win with no async/callback rewrite.

check yourself

Quick quiz

Q1

What happens to a virtual thread when it makes a blocking I/O call?

Q2

Why can you have millions of virtual threads but only thousands of platform threads?

follow the thread