Sub topic 3
What a virtual thread is
This chapter assumes you know Java 21 basics, including ExecutorService, and that you understand blocking I/O such as JDBC calls and HTTP clients. It covers the working model of a virtual thread and the errors people make with it. Later chapters cover scheduling details, Spring Boot configuration, and pinning.
The idea in one paragraph
A virtual thread is a java.lang.Thread that the JVM schedules, not the operating system. It is defined in JEP 444 and final in Java 21. Your code still reads as plain, blocking, step-by-step Java. The difference is cost. A platform thread is a thin wrapper around an OS thread and reserves stack memory up front. A virtual thread keeps its stack as small objects on the heap and grows as needed. You can create hundreds of thousands of them.
Platform threads and virtual threads side by side
| Property | Platform thread | Virtual thread |
|---|---|---|
| Backed by | One OS thread | JVM-managed, borrows an OS thread only while running |
| Typical count | Hundreds to a few thousand | Hundreds of thousands or more |
| Creation cost | High, so threads are pooled | Low, so threads are not pooled |
| Stack | Fixed reservation | Heap objects that grow and shrink |
| Daemon status | Configurable | Always daemon |
| Priority | Configurable | Fixed at normal |
The last two rows matter in practice. A virtual thread never keeps the JVM alive, and setting its priority has no effect.
What happens when it blocks
When a virtual thread calls a blocking operation such as a socket read, Thread.sleep, or a BlockingQueue.take, the JVM saves its stack to the heap and frees the OS thread it was using. That OS thread, called a carrier, can then run another virtual thread. When the I/O completes, the JVM schedules the virtual thread again. Your code never sees this. The next chapter explains carriers in detail.
Creating one
You can create virtual threads directly, with no Spring involved.
public class HelloVirtual {
public static void main(String[] args) throws InterruptedException {
Thread thread = Thread.ofVirtual()
.name("worker-1")
.start(() -> System.out.println(
"virtual=" + Thread.currentThread().isVirtual()));
thread.join();
}
}The program prints virtual=true. Other entry points are Thread.startVirtualThread(Runnable) and Executors.newVirtualThreadPerTaskExecutor(). All of them return ordinary Thread or ExecutorService types, so existing code that accepts those keeps working.
Common errors
- Pooling virtual threads. A pool exists to limit how many expensive threads you create. Virtual threads are cheap, so create one per task. If you need to limit concurrency, use a
Semaphore, which a later chapter covers. - Expecting faster single requests. A virtual thread does not make one blocking call finish sooner. It lets many blocked calls wait at the same time without using an OS thread each. The gain is throughput under high concurrency.
- Using them for CPU-bound work. A thread that computes without ever blocking never unmounts. Virtual threads add nothing there, because the number of CPU cores still limits progress.
- Assuming a pause equals a blocked carrier. Most JDK blocking calls unmount the virtual thread. Some situations do not, such as blocking inside a
synchronizedblock on Java 21. This is called pinning and has its own chapter.
Checking what you have
Thread.currentThread().isVirtual() is the quickest test. Spring Boot setup comes later, but this is the check you will reuse to confirm that a request is running on a virtual thread.
Exercise
A service handles 5,000 concurrent requests, and each spends 200 ms waiting on a database. A colleague proposes a fixed pool of 5,000 virtual threads to match. What is wrong with this design, and what would you use instead?
Show answer
Virtual threads are cheap, so they should not be pooled. Create one per task, for example with Executors.newVirtualThreadPerTaskExecutor(). If the database can only handle a limited number of simultaneous calls, enforce that limit with a Semaphore (or the connection pool size) rather than by limiting the number of threads.