Sub topic 6
Under the Hood: Carrier Threads and Mounting
So far you have started virtual threads and watched thousands of them finish quickly. This chapter shows what the JVM does to make that possible. The JVM is the Java Virtual Machine, the program that runs your Java code.
The Key Idea: Virtual Threads Borrow Real Threads
A virtual thread cannot run on the CPU by itself. It needs a platform thread to run on. A platform thread is a regular Java thread that is backed by a thread in the operating system.
The platform thread that a virtual thread runs on is called its carrier thread. You can think of the carrier as a car and the virtual thread as a passenger. A small number of cars can move many passengers, as long as passengers step out when they are waiting.
The Scheduler
The scheduler is the part of the JDK that decides which virtual thread runs on which carrier. By default it is a special ForkJoinPool, which is a pool of platform threads built into the JDK.
- The pool starts with about one carrier per CPU core. On an 8-core machine, you get about 8 carriers.
- Waiting virtual threads take their turn in first-in, first-out order.
- You almost never need to configure it.
Mounting and Unmounting
Two words describe what happens to a virtual thread:
| Term | Meaning |
|---|---|
| Mount | The scheduler places a virtual thread on a carrier, and its code starts or continues running. |
| Unmount | The virtual thread steps off the carrier. Its current state is saved in ordinary Java memory (the heap). |
| Carrier | The platform thread a virtual thread is mounted on. |
| Scheduler | The pool that hands out carriers to virtual threads. |
When a virtual thread blocks, for example by calling Thread.sleep() or waiting for a network reply, it usually unmounts. Blocking means waiting for something before it can go on. The carrier is now free, so the scheduler mounts a different virtual thread on it. When the wait is over, the first virtual thread is mounted again. It may land on a different carrier than before.
Watching Carriers Change
When you print a virtual thread, its text includes the carrier it is mounted on right now. This program prints each thread before and after a short sleep.
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
public class WatchCarriers {
public static void main(String[] args) throws InterruptedException {
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < 4; i++) {
threads.add(Thread.ofVirtual().start(() -> {
System.out.println("before: " + Thread.currentThread());
try {
Thread.sleep(Duration.ofMillis(100));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("after: " + Thread.currentThread());
}));
}
for (Thread t : threads) {
t.join();
}
}
}A typical line of output looks like this:
before: VirtualThread[#22]/runnable@ForkJoinPool-1-worker-2
after: VirtualThread[#22]/runnable@ForkJoinPool-1-worker-4The part before @ is the virtual thread, number 22. The part after @ is the carrier. In this run, thread #22 started on worker-2 and finished on worker-4. The exact names change from run to run.
Why This Makes Blocking Cheap
A sleeping platform thread holds on to an operating system thread the whole time it waits. A sleeping virtual thread holds nothing but a small saved state on the heap. That is why 10,000 sleeping tasks fit on just a few carriers. The next chapter looks at which blocking operations unmount cleanly.
Exercise
A virtual thread prints "worker-1" as its carrier, then calls Thread.sleep(), then prints "worker-3". Explain what happened in between.
Show answer
When the virtual thread called sleep, it unmounted from carrier worker-1, and its state was saved on the heap. Worker-1 was then free to run other virtual threads. When the sleep ended, the scheduler mounted the virtual thread again on whichever carrier was free, which this time was worker-3.
Exercise
On a 4-core machine, about how many carrier threads does the default scheduler start with, and why can it still run 10,000 sleeping virtual threads?
Show answer
About 4, one per core. Sleeping virtual threads unmount and do not use a carrier while they wait, so the 4 carriers only run threads that are ready to do work.