What Is a Java Virtual Thread? Platform Threads, Virtual Threads and How They Differ
The two kinds of Java threads explained: OS-backed platform threads and JVM-scheduled virtual threads. How carriers and the ForkJoinPool scheduler work, which Thread properties are fixed for virtual threads, what a virtual thread costs, and how to tune the scheduler.
kvi5hnuCode Sprout team member8 min readOn this page
Since Java 21, java.lang.Thread comes in two kinds. Platform threads are the threads Java has always had, each one a wrapper around an operating-system thread. Virtual threads are scheduled by the JVM itself, so they're cheap enough to create one for every task, even millions of them. This article explains what a virtual thread is, how it differs from a platform thread, and which parts of the Thread API behave differently.
Two kinds of thread, one API
Platform threads (also called classic, native or OS threads) map one to one onto kernel threads. The OS scheduler decides when each runs, and each has a fixed-size native stack. They're expensive to create and to keep, and the OS limits how many can exist.
Virtual threads (sometimes called user-mode or lightweight threads) aren't tied to a kernel thread. The JVM multiplexes many virtual threads onto a small number of platform threads called carriers. While a virtual thread runs Java code, it's mounted on a carrier. When it blocks, the JVM unmounts it, stores its stack frames on the Java heap, and lets the carrier run another virtual thread.

Both are instances of java.lang.Thread. A virtual thread has a name, an ID, an interrupt flag, thread-local variables, an uncaught exception handler and a stack trace, and Thread.currentThread() returns it, never its carrier. Code that uses threads mostly can't tell which kind it's running on.
How they differ
| Platform thread | Virtual thread | |
|---|---|---|
| Scheduled by | the OS kernel | the JVM (a ForkJoinPool) |
| Mapping | 1 Java thread : 1 kernel thread | many virtual threads : few carriers |
| Stack | fixed-size native stack, 1–2 MiB reserved | heap objects that grow and shrink, often under 1 KiB |
| Creation cost | a system call, tens of microseconds | an object allocation, around a microsecond |
| How many | thousands | millions |
| Blocking | holds the kernel thread while waiting | unmounts and frees the carrier for other work |
| Daemon | configurable, inherited from the creator | always a daemon |
| Priority | configurable (a hint to the OS) | always NORM_PRIORITY (5) |
| Pool them? | yes, creation is expensive | no, create one per task |
The most important row is blocking. Reading from a socket, calling Thread.sleep, waiting on a BlockingQueue or a ReentrantLock: on a virtual thread, these all release the carrier. That's what makes thread-per-request code scale.
The scheduler
Virtual threads are scheduled by a dedicated ForkJoinPool, separate from the common pool that parallel streams use. Two details matter:
- It runs in FIFO mode: ready virtual threads are taken in the order they became ready, which is fair for tasks that block and resume. Work stealing still balances load across carriers.
- Its parallelism, the number of carriers actively running virtual threads, defaults to the number of available processors.
You can tune the scheduler with system properties, set on the command line:
java -Djdk.virtualThreadScheduler.parallelism=4 -jar app.jar
| Property | Default | Meaning |
|---|---|---|
jdk.virtualThreadScheduler.parallelism | available processors | carriers that run virtual threads at the same time |
jdk.virtualThreadScheduler.maxPoolSize | max(parallelism, 256) | upper limit on carriers, including temporary extras added while a few carrier-blocking operations, such as synchronous file writes or DNS lookups, run |
The scheduler reads these once, when it's created, which happens when the first virtual thread is created. Setting them with System.setProperty works only if it runs before any virtual thread is created anywhere in the JVM, including inside frameworks, so the command line is the safer place. In practice, most applications never change the defaults.
Which Thread properties are fixed
Some properties of a virtual thread can't be changed:
- Always a daemon. Virtual threads never keep the JVM alive.
setDaemon(false)throwsIllegalArgumentException. Ifmainreturns without waiting for its virtual threads, the JVM exits and they simply stop. - Fixed priority.
getPriority()always returns 5, andsetPriorityis silently ignored. The JVM scheduler doesn't use priorities. - One thread group. All virtual threads belong to a group named
VirtualThreads. There's no way to put a virtual thread in another group. Thread groups are a legacy feature anyway.
The program below checks all of these. Thread IDs and carrier numbers change from run to run, so the program replaces them with N:
public class Solution {
// Thread IDs and carrier numbers vary between runs, so replace them with N
static String tidy(Object o) {
return o.toString().replaceAll("#\\d+", "#N").replaceAll("worker-\\d+", "worker-N");
}
public static void main(String[] args) throws InterruptedException {
Thread vt = Thread.ofVirtual().name("quote-7").unstarted(() ->
System.out.println("running as: " + tidy(Thread.currentThread())));
Thread pt = Thread.ofPlatform().name("batch-1").unstarted(() -> {});
System.out.println("isVirtual: virtual=" + vt.isVirtual() + ", platform=" + pt.isVirtual());
System.out.println("daemon: virtual=" + vt.isDaemon() + ", platform=" + pt.isDaemon());
System.out.println("thread group: virtual=" + vt.getThreadGroup().getName()
+ ", platform=" + pt.getThreadGroup().getName());
vt.setPriority(Thread.MAX_PRIORITY); // silently ignored
System.out.println("priority: " + vt.getPriority() + " after setPriority(10)");
try {
vt.setDaemon(false);
} catch (IllegalArgumentException e) {
System.out.println("setDaemon: " + e.getMessage());
}
vt.start();
vt.join();
System.out.println("after join: " + tidy(vt));
System.out.println("in getAllStackTraces: " + Thread.getAllStackTraces().containsKey(vt));
}
}
Output:
isVirtual: virtual=true, platform=false
daemon: virtual=true, platform=false
thread group: virtual=VirtualThreads, platform=main
priority: 5 after setPriority(10)
setDaemon: 'false' not legal for virtual threads
running as: VirtualThread[#N,quote-7]/runnable@ForkJoinPool-1-worker-N
after join: VirtualThread[#N,quote-7]/terminated
in getAllStackTraces: false
Two lines are worth a closer look:
toString()shows the carrier. While the virtual thread runs, its string form ends with@ForkJoinPool-1-worker-N, the carrier it's mounted on at that moment. There's no API that returns the carrier as aThreadobject, by design: code shouldn't depend on which carrier it runs on, because a virtual thread can move to a different carrier every time it blocks and resumes.getAllStackTraces()ignores virtual threads. It returns platform threads only, since a process can have millions of virtual threads. To see virtual threads, use a thread dump fromjcmd(article 17).
What a virtual thread costs
A virtual thread's stack is a chain of heap objects holding only the frames it actually uses, so a thread blocked in a shallow call chain needs very little memory. This program starts 100,000 virtual threads that all block on a latch and measures the heap before and after:
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
public class VirtualFootprint {
static long usedHeap() {
System.gc();
Runtime rt = Runtime.getRuntime();
return rt.totalMemory() - rt.freeMemory();
}
public static void main(String[] args) throws InterruptedException {
int count = 100_000;
CountDownLatch release = new CountDownLatch(1);
CountDownLatch parked = new CountDownLatch(count);
long before = usedHeap();
long start = System.nanoTime();
List<Thread> threads = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
threads.add(Thread.ofVirtual().start(() -> {
parked.countDown();
try { release.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
}));
}
parked.await();
long startMs = (System.nanoTime() - start) / 1_000_000;
long after = usedHeap();
System.out.printf("%,d blocked virtual threads started in %d ms%n", count, startMs);
System.out.printf("extra heap: %,d KiB, about %,d bytes per thread%n", (after - before) / 1024, (after - before) / count);
System.out.println("OS threads in the JVM: " + java.lang.management.ManagementFactory.getThreadMXBean().getThreadCount());
release.countDown();
for (Thread t : threads) t.join();
}
}
On JDK 25 on a 10-core laptop, it printed:
100,000 blocked virtual threads started in 108 ms
extra heap: 103,796 KiB, about 1,062 bytes per thread
OS threads in the JVM: 17
About 1 KiB per blocked thread (including the Thread object, its stack chunk and the latch's wait-queue node), and still only 17 OS threads in the whole JVM. The same experiment with platform threads would need 100,000 OS threads, which most machines can't create at all. Threads with deeper stacks cost more, since the heap holds every frame, but the cost is proportional to what each thread actually uses.
Changes to the Thread API
Virtual threads came with a few API updates that apply to all threads:
Thread.ofVirtual()andThread.ofPlatform()return builders, andThread.startVirtualThread(Runnable)starts one directly. Article 9 covers them.isVirtual()tells you which kind a thread is.join(Duration)andsleep(Duration)(Java 19) accept ajava.time.Duration.threadId()(Java 19) is final and replacesgetId(), which subclasses could override and is now deprecated.stop()always throwsUnsupportedOperationException(since Java 20), andsuspend(),resume()andcountStackFrames()have been removed.
What's current
- Virtual threads have been final since Java 21 (JEP 444) and are unchanged in their basic API through Java 27.
- Java 24 (JEP 491) lets virtual threads unmount while inside
synchronizedcode, which removed the most common cause of pinning. Article 13 covers pinning. - OpenJDK is experimenting with allowing custom virtual-thread schedulers. The built-in
ForkJoinPoolscheduler is the only supported one in Java 25 to 27.
Common questions
Is a virtual thread a green thread? In spirit, yes: both are scheduled by the runtime instead of the OS. But early Java green threads (before Java 1.3) ran everything on one OS thread, so they couldn't use multiple cores. Virtual threads run on as many carriers as there are cores, and blocking I/O unmounts them instead of blocking the whole process.
Can I get the carrier thread of a virtual thread?
Not as an object. The carrier's name appears in the virtual thread's toString() and in thread dumps for diagnostics. Code must not depend on the carrier, because a virtual thread can resume on a different one after every blocking call.
Why does my program exit before my virtual thread prints anything?
Virtual threads are always daemon threads, so the JVM doesn't wait for them. Join the thread, use an executor's close(), or use structured concurrency to wait for the work to finish.
Are virtual threads faster than platform threads? No. Code runs at the same speed on both. Virtual threads improve throughput when many tasks wait at the same time, because waiting no longer costs an OS thread. A single request isn't faster, and CPU-bound work gains nothing.
Summary
- Java has two kinds of thread: platform threads (1 with kernel threads, OS-scheduled) and virtual threads (many-to-few onto carrier platform threads, JVM-scheduled).
- A blocked virtual thread unmounts from its carrier, which then runs another virtual thread.
- The scheduler is a dedicated FIFO
ForkJoinPoolwith one carrier per core by default, tunable withjdk.virtualThreadScheduler.parallelismandmaxPoolSize. - Virtual threads are always daemons, always
NORM_PRIORITY, and all in theVirtualThreadsgroup.setDaemon(false)throws. - A blocked virtual thread with a shallow stack costs about 1 KiB of heap, so millions are practical.
getAllStackTraces()doesn't include virtual threads. Usejcmdthread dumps instead.
Practice questions
What does this program print?
public class Solution {
public static void main(String[] args) {
Thread.startVirtualThread(() -> System.out.println("hello from a virtual thread"));
}
}
Answer
Usually nothing. main returns right after starting the thread, and since a virtual thread is a daemon, the JVM exits without waiting for it. The line is printed only if the virtual thread happens to run before the JVM shuts down. Keep the returned Thread and call join().
A developer calls vt.setPriority(Thread.MAX_PRIORITY) on a virtual thread to make an urgent task run first. Does it help?
Answer
No. The call is silently ignored and getPriority() still returns 5. The virtual-thread scheduler takes ready threads in FIFO order and doesn't use priorities. To prioritize work, use separate executors or queues for urgent tasks.
A service starts with -Djdk.virtualThreadScheduler.parallelism=2 on a 16-core machine. What is the effect?
Answer
At most two virtual threads run Java code at the same time, on two carriers (plus temporary extras during a few carrier-blocking operations). Blocking virtual threads still unmount, so thousands can wait, but CPU work in virtual threads can use only two cores.
Previous: Reactive Programming vs Virtual Threads in Java: Trade-offs and the Promise of Project Loom
Next: Creating Java Virtual Threads: startVirtualThread, Thread.Builder, Factories and Executors

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.