Sub topic 3
Meet Virtual Threads
The last two chapters showed a problem. Platform threads are expensive, so a server can only have a few thousand of them. When each request needs its own thread, the server runs out of threads long before it runs out of work. Virtual threads solve this problem.
What a virtual thread is
A virtual thread is a Java thread that the Java Virtual Machine (JVM) manages by itself, without giving it a dedicated operating system thread. The JVM is the program that runs your Java code.
A platform thread is the traditional kind of Java thread. Each platform thread is a thin wrapper around one operating system thread, and it keeps that OS thread for its whole life.
A virtual thread works differently. It borrows a platform thread only while it is actually running code. When it has to wait, for example for a network reply, it gives the platform thread back. Another virtual thread can then use it.
Virtual threads became a standard part of Java in Java 21, through a proposal called JEP 444. A JEP (JDK Enhancement Proposal) is the document that describes a new Java feature.
Same API, different cost
A virtual thread is still a java.lang.Thread object. You call the same methods on it, such as start(), join(), and getName(). Most existing code does not need to know which kind of thread it runs on.
The difference is cost:
| Feature | Platform thread | Virtual thread |
|---|---|---|
| Backed by | One OS thread, for its whole life | A borrowed platform thread, only while running |
| Memory for its stack | About 1 MB reserved up front | Small, and it grows only as needed |
| Created and scheduled by | The operating system | The JVM |
| How many you can have | Thousands | Millions |
| Good to pool and reuse? | Yes | No, create a new one per task |
A stack is the memory a thread uses to remember which methods it is in and what their local variables hold. A virtual thread keeps its stack in ordinary Java heap memory, so the stack can start small.
How the pieces fit together
The platform threads that run virtual threads are called carrier threads. A later chapter explains how the scheduler moves virtual threads on and off them.
A worked example
Suppose your server gets 10,000 requests at once. Each request waits 1 second for a database reply.
- With platform threads, you need 10,000 OS threads. At about 1 MB of reserved stack each, that is roughly 10 GB of address space. Most systems slow down or refuse well before that, so in practice you use a pool of perhaps 200 threads. The other requests wait in a queue, and the last ones finish after about 50 seconds (10,000 ÷ 200).
- With virtual threads, you create 10,000 virtual threads. While each one waits for the database, it gives its carrier back. A handful of carriers can serve all of them, and every request finishes after about 1 second.
The work itself did not get faster. Waiting just stopped using up a scarce resource.
What stays the same
You still write ordinary, step-by-step blocking code. You do not need callbacks or special reactive libraries. Exceptions, debuggers, and stack traces work as they do with platform threads. This is the main appeal: you keep the simple thread-per-request style, and it now scales.
Exercise
A virtual thread is waiting for a reply from a web service. What happens to the platform thread it was running on?
Show answer
The virtual thread gives the platform thread (its carrier) back to the JVM while it waits. The carrier can then run a different virtual thread. When the reply arrives, the waiting virtual thread is scheduled to run again on a carrier.
Exercise
Your program calculates prime numbers on all CPU cores and never waits for I/O. Will switching to virtual threads make it much faster? Why or why not?
Show answer
No. Virtual threads help when many tasks are waiting. This program is busy computing all the time, and the number of CPU cores limits it. More threads cannot add more cores.