The Virtual Thread Scheduler: How ForkJoinPool Runs Java Virtual Threads
How the JDK schedules virtual threads: a dedicated ForkJoinPool in FIFO async mode whose workers are the carriers, the parallelism and maxPoolSize settings, compensation for blocking calls, and the read and write pollers and delay scheduler that wake virtual threads up. Verified with jcmd on JDK 25.
kvi5hnuCode Sprout team member8 min readOn this page
A virtual thread needs two things from the JDK: something to run it on a carrier when it's ready, and something to wake it up when whatever it waits for has happened. The first is the virtual-thread scheduler, a specially configured ForkJoinPool. The second is a small set of helpers: I/O pollers, a timer and ordinary unpark calls. This article looks at both.

Why a ForkJoinPool
The scheduler's job is simple to state: keep about one carrier per core busy running whichever virtual threads are ready. A ForkJoinPool is a good fit for three reasons:
- Low contention. Each worker has its own deque, and workers steal from each other using CAS (article 22). With hundreds of thousands of virtual threads becoming ready and blocking again, a single locked queue would become the bottleneck.
- Locality. A virtual thread woken by code running on a carrier is usually pushed onto that carrier's own deque, so it tends to run where its data is still in the cache.
- Compensation.
ForkJoinPoolalready knows how to add a temporary worker when one is blocked (managedBlock), which the scheduler uses for the few operations that hold a carrier (article 14).
It's a separate pool from the common pool, so parallel streams and virtual threads never compete for workers.
FIFO: asyncMode
A ForkJoinPool normally processes a worker's own deque in LIFO order: the most recently forked task runs first, which suits divide and conquer. For independent event-like tasks, it can be created in asyncMode, which makes local processing FIFO. The virtual-thread scheduler uses asyncMode, so ready virtual threads run roughly in the order they became ready, which is fairer for request handling.
Seeing the scheduler
The program checks which pool carries virtual threads, shows LIFO versus FIFO on a one-worker pool, and blocks 100 virtual threads on real socket reads:
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
public class Solution {
/** One worker; a task forks five subtasks onto the worker's own deque. In what order do they run? */
static List<Integer> localOrder(boolean asyncMode) throws InterruptedException {
List<Integer> order = new CopyOnWriteArrayList<>();
CountDownLatch done = new CountDownLatch(1);
try (ForkJoinPool pool = new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, asyncMode)) {
pool.execute(() -> {
for (int i = 1; i <= 5; i++) {
int id = i;
ForkJoinTask.adapt(() -> order.add(id)).fork(); // pushed onto this worker's deque
}
ForkJoinTask.helpQuiesce(); // the worker itself runs its local tasks
done.countDown();
});
done.await(); // main waits without helping the pool
}
return order;
}
static String carrierPool() { // e.g. "ForkJoinPool-1" from ".../runnable@ForkJoinPool-1-worker-3"
String s = Thread.currentThread().toString();
String carrier = s.substring(s.indexOf('@') + 1);
return carrier.substring(0, carrier.lastIndexOf("-worker"));
}
public static void main(String[] args) throws Exception {
// 1. Virtual threads run on their own ForkJoinPool, not the common pool
String[] where = new String[1];
Thread.ofVirtual().start(() -> where[0] = carrierPool()).join();
String common = ForkJoinPool.commonPool().submit(() -> Thread.currentThread().getName()).get();
System.out.println("virtual threads are carried by: " + where[0]
+ "; the common pool's threads are named like: " + common.replaceAll("\\d+$", "N"));
// 2. Default (LIFO) versus asyncMode (FIFO) for locally forked tasks
System.out.println("default mode, local tasks ran in order: " + localOrder(false));
System.out.println("asyncMode, local tasks ran in order: " + localOrder(true));
// 3. 100 virtual threads block reading sockets; the read poller wakes them, carriers stay few
Set<String> carriers = ConcurrentHashMap.newKeySet();
try (ServerSocket server = new ServerSocket(0);
ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
Thread.ofVirtual().start(() -> { // a slow server: answers after 200 ms
try {
while (true) {
Socket s = server.accept();
executor.submit(() -> { try (s) { Thread.sleep(200); s.getOutputStream().write(42); } return null; });
}
} catch (IOException closed) { /* server closed: stop accepting */ }
});
long start = System.nanoTime();
List<java.util.concurrent.Future<Integer>> replies = new java.util.ArrayList<>();
for (int i = 0; i < 100; i++) {
replies.add(executor.submit(() -> {
try (Socket client = new Socket("localhost", server.getLocalPort())) {
InputStream in = client.getInputStream();
int b = in.read(); // blocks: the virtual thread unmounts here
String s = Thread.currentThread().toString();
carriers.add(s.substring(s.indexOf('@') + 1));
return b;
}
}));
}
int ok = 0;
for (var r : replies) if (r.get() == 42) ok++;
long ms = (System.nanoTime() - start) / 1_000_000;
System.out.println(ok + " socket replies after " + (ms < 1_000 ? "under 1 s: the 200 ms waits overlapped" : ms + " ms") + ", carriers used <= cores: "
+ (carriers.size() <= Runtime.getRuntime().availableProcessors()));
}
}
}
Output:
virtual threads are carried by: ForkJoinPool-1; the common pool's threads are named like: ForkJoinPool.commonPool-worker-N
default mode, local tasks ran in order: [5, 4, 3, 2, 1]
asyncMode, local tasks ran in order: [1, 2, 3, 4, 5]
100 socket replies after under 1 s: the 200 ms waits overlapped, carriers used <= cores: true
The scheduler is its own pool (ForkJoinPool-1, created when the first virtual thread starts), distinct from the common pool. The same five forked tasks ran newest-first in default mode and oldest-first in asyncMode. And 100 virtual threads, each blocked in a socket read(), all got their replies in well under a second, because their 200 ms waits overlapped, carried by no more carriers than there are cores.
Scheduler settings
The scheduler reads three system properties, which must be set before the first virtual thread is created (normally as -D options):
| Property | Default | Meaning |
|---|---|---|
jdk.virtualThreadScheduler.parallelism | number of cores | how many carriers normally run virtual threads |
jdk.virtualThreadScheduler.maxPoolSize | max(parallelism, 256) | the most carriers allowed, including compensation threads |
jdk.virtualThreadScheduler.minRunnable | max(parallelism / 2, 1) | the minimum number of carriers kept runnable while others are blocked |
You rarely need to change them. Lowering parallelism to 1 is a useful trick in tests, as articles 13 and 14 did, to make scheduling effects visible. As of Java 27 there is no supported API for plugging in your own scheduler.
To look at a running scheduler, jcmd <pid> Thread.vthread_scheduler prints the pool's state. During a run with 50 virtual threads blocked on sockets and sleeping, it showed:
java.util.concurrent.ForkJoinPool@50567d2a[Running, parallelism = 10, size = 10, active = 0, running = 0, steals = 203, tasks = 0, submissions = 0, delayed = 50]
active = 0 means no carrier was running a virtual thread at that moment: everything was waiting, and the waiting cost no carriers. delayed = 50 counts pending timers.
What wakes virtual threads up
A blocked virtual thread unmounts and leaves behind a way to be resumed. Every blocking point in the JDK ends in LockSupport.park(), which for a virtual thread yields its continuation (article 24) instead of blocking the carrier. Something must later call unpark:
- Sockets: the pollers. When a socket operation (
read,connect,accept,write) can't complete immediately on a virtual thread, the JDK registers the socket's file descriptor with a poller and parks. A poller is an event loop over the OS's readiness API (kqueue on macOS, epoll on Linux, wepoll on Windows) with a map from file descriptors to waiting virtual threads. When the OS reports the socket is ready, the poller unparks that thread.jcmd <pid> Thread.vthread_pollerslists them. During the run above it showed oneRead-Pollerwith 51 registered sockets (50 client reads plus the server'saccept) and oneWrite-Poller. - Timers: the delay scheduler.
Thread.sleep,parkNanosand timed waits schedule a wake-up. In Java 25 the scheduler'sForkJoinPoolhandles these delayed tasks itself, using aForkJoinPool-1-delaySchedulerthread. Java 21 used a separate scheduled executor for this. - Everything else: other threads. Locks, queues, latches and futures record waiting threads and
unparkthem when the condition changes, the same as for platform threads.
Whichever source wakes a virtual thread, it does the same thing: it submits the virtual thread's continuation back to the scheduler. The next free carrier mounts it and continues from where it parked.
Compensation
For the few operations that block the carrier itself, such as synchronous file writes, reading or writing a child process's streams, DNS lookups and (on Java 21–23) Object.wait(), the JDK tells the pool that a carrier is about to block. The pool may then start an extra carrier, up to maxPoolSize, so the other virtual threads keep running. When the operation ends, the extra carrier eventually retires. This is the same mechanism as ForkJoinPool.managedBlock, applied automatically inside the JDK.
What's current
- The scheduler is a dedicated
ForkJoinPoolin asyncMode on every release since Java 21, and its three system properties are unchanged. - Java 25's
ForkJoinPoolgained delayed and periodic scheduling, which the virtual-thread scheduler uses for sleeps and timeouts. jcmd Thread.vthread_schedulerandThread.vthread_pollersare available in recent JDKs, including Java 25, for inspecting the scheduler and pollers.- Poller internals have been reworked between releases and differ by platform. They're not configurable through supported options.
Common questions
Can I give virtual threads a different executor?
Not through a supported API, as of Java 27. Thread.ofVirtual() always uses the built-in scheduler. Custom schedulers have been explored in Project Loom's early-access builds, but aren't part of any release.
Why does the scheduler use FIFO instead of LIFO? Virtual threads are independent tasks, like events, not pieces of a recursive computation. FIFO order is fairer: a thread that became ready earlier runs earlier, so latency is more even across requests.
Does a busy parallel stream slow down virtual threads? Not by taking their carriers: parallel streams use the common pool, and virtual threads use their own pool. Both compete for the same CPU cores, though, so a CPU-heavy stream still affects everything on the machine.
How many poller threads are there? It depends on the platform and release. On the JDK 25 macOS run above there was one read poller and one write poller. Each poller handles many sockets, so the number stays small no matter how many virtual threads wait.
Summary
- The virtual-thread scheduler is a dedicated
ForkJoinPoolin asyncMode (FIFO), whose workers are the carrier threads. - By default it has one carrier per core, may grow to 256 for compensation, and is tuned with
jdk.virtualThreadScheduler.*properties. - Blocked virtual threads are woken by pollers (sockets, using kqueue, epoll or wepoll), the delay scheduler (sleeps and timeouts) or
unparkfrom other threads, each of which resubmits the thread to the pool. - Operations that block the carrier trigger compensation, a temporary extra carrier.
jcmd Thread.vthread_schedulerandThread.vthread_pollersshow the scheduler and pollers of a running JVM.
Practice questions
A test sets -Djdk.virtualThreadScheduler.parallelism=1. What does that change, and why might a test want it?
Answer
With parallelism 1, virtual threads share a single carrier, so only one runs at a time (compensation can still add a temporary carrier for operations that block it). Blocking calls still unmount, so concurrency for waiting work is unaffected. Tests use it to make scheduling effects visible and repeatable, for example to show that a pinned thread blocks every other virtual thread.
jcmd <pid> Thread.vthread_scheduler shows parallelism = 8, size = 8, active = 8 for a long time, while thousands of requests are slow. What does it suggest?
Answer
All eight carriers are busy running virtual threads, so ready threads wait for a carrier. Either the work is CPU-bound (no time slicing), or carriers are pinned in long native calls or class initializers. Check JFR pinning events and CPU profiles, and move long CPU or native work to a separate pool.
Which component unparks a virtual thread that is waiting in socket.getInputStream().read() when data arrives?
Answer
The read poller. When the read couldn't complete, the JDK registered the socket's file descriptor with the poller and parked the virtual thread. When the OS (through kqueue, epoll or wepoll) reports the socket readable, the poller unparks the thread, which resubmits it to the scheduler.
Previous: ForkJoinPool in Java: Work Stealing, RecursiveTask and Why Ordinary Pools Deadlock
Next: Continuations in Java: How Virtual Threads Pause and Resume

More in Modern Java Concurrency
Quiz: Java Concurrency Changes From Java 21 to Java 28
Sixteen questions on what changed release by release: virtual threads final in 21, synchronized without pinning in 24, scoped values final in 25, and the long road of structured concurrency from subclasses to open() and Joiner, the Java 26 and 27 refinements, and final in 28.
Quiz: Java Concurrency's Delicate Details
Twenty edge cases of modern Java concurrency that surprise experienced developers: virtual thread priorities and names, semaphores without owners, null scoped values, cancelling a CompletableFuture, pool sizing with unbounded queues, and StructuredTaskScope's strict rules.
Quiz: Java Concurrency Interview Questions (Virtual Threads, Structured Concurrency, Scoped Values)
Twenty questions of the kind interviewers ask about modern Java concurrency: platform vs virtual threads, pinning, pools and limits, CompletableFuture, structured concurrency, scoped values vs ThreadLocal, reactive programming, and production diagnostics.
Quiz: Modern Java Concurrency Final Exam (All 7 Parts)
The final exam for the modern Java concurrency course: thirty questions across all seven parts, from platform threads, pools and CompletableFuture through virtual threads, the scheduler, structured concurrency, scoped values, reactive streams and frameworks.
Discussion
Sign in to ask a question or share what you learned.