Sub topic 4
Carrier threads: how virtual threads run
This chapter assumes you know Java 21 basics, ExecutorService, and why a blocking call such as JDBC or an HTTP client holds a thread while it waits. The previous chapter defined a virtual thread. Here you will see what actually executes it.
Virtual threads need a platform thread to run
A virtual thread is a Thread object managed by the JVM. The operating system does not know about it. The CPU can only run OS threads, so every virtual thread must borrow one to execute code. The borrowed OS thread is called a carrier thread.
The JVM keeps a small pool of carrier threads. This pool is a ForkJoinPool running in FIFO mode. By default its size equals the number of available processors, so an 8-core machine has about 8 carriers. You can change this with system properties such as jdk.virtualThreadScheduler.parallelism and jdk.virtualThreadScheduler.maxPoolSize, but most applications never need to.
Thousands of virtual threads share those few carriers. That sharing is why virtual threads are cheap.
Mounting and unmounting
Two terms describe how a virtual thread moves between states:
- Mount: the scheduler assigns the virtual thread to a carrier. Its stack frames are copied onto the carrier, and your code runs.
- Unmount: the virtual thread gives up the carrier. Its stack frames are copied to the heap, and the carrier is free to run another virtual thread.
A virtual thread unmounts when it hits a blocking operation that the JDK knows how to handle, such as Thread.sleep, a socket read, BlockingQueue.take, or Lock.lock on a ReentrantLock. When the operation can continue, for example when data arrives on the socket, the scheduler mounts the virtual thread again. It may land on a different carrier than before.
A worked example
The program below starts 1,000 virtual threads. Each one sleeps for one second, then reports which carrier ran it. The carrier name appears in the thread's toString() output.
import java.time.Duration;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
public class CarrierDemo {
public static void main(String[] args) {
AtomicInteger printed = new AtomicInteger();
long start = System.nanoTime();
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < 1_000; i++) {
executor.submit(() -> {
Thread.sleep(Duration.ofSeconds(1));
if (printed.incrementAndGet() <= 3) {
System.out.println(Thread.currentThread());
}
return null;
});
}
}
long millis = (System.nanoTime() - start) / 1_000_000;
System.out.println("Finished in about " + millis + " ms");
}
}Typical output looks like this, although the carrier numbers will differ on your machine:
VirtualThread[#29]/runnable@ForkJoinPool-1-worker-3
VirtualThread[#35]/runnable@ForkJoinPool-1-worker-1
VirtualThread[#41]/runnable@ForkJoinPool-1-worker-2
Finished in about 1100 msAll 1,000 sleeps overlap, so the run takes a little over one second. With a platform thread pool of 8 threads, the same work would take about 125 seconds. The ForkJoinPool-1-worker-N suffix is the carrier. Only a handful of carriers exist, yet 1,000 tasks made progress at the same time.
What this means for Spring Boot
When you set spring.threads.virtual.enabled=true, each incoming request gets a virtual thread. Your controller code runs on a carrier only while it is computing. While it waits on the database or a remote service, it unmounts, and the carrier serves other requests.
Two consequences follow:
- CPU-bound work still needs cores. A virtual thread that computes without blocking never unmounts. It holds its carrier until it finishes, so a few such tasks can occupy every carrier.
- Carriers are a scarce resource. Anything that stops a virtual thread from unmounting while it waits ties up a carrier. The pinning chapter covers this case.
Exercise
A service runs on a 4-core machine with default scheduler settings. 500 requests arrive at once, and each blocks for two seconds on a database call. How many carrier threads run the requests, and why can all 500 wait at the same time?
Show answer
About 4 carrier threads, because the default parallelism equals the processor count. All 500 can wait together because each virtual thread unmounts from its carrier when it blocks on the I/O call, which frees the carrier for other work. The waiting virtual threads are stored on the heap and need no carrier. Note that the database still sees 500 simultaneous requests unless you limit them.