Lesson · Virtual Threads & Structured Concurrency
Platform vs Virtual Threads: a wrapper or a task
15 min readPlatform Threads vs Virtual Threads
A platform thread is an OS thread with a Java name: the kernel schedules it, it carries roughly a megabyte of stack, and tens of thousands of them is a fight. A virtual thread is a task the JVM schedules: many of them ride a few carrier threads, and a parked one costs heap rather than a kernel thread. This lesson draws both mappings and shows why waiting got cheap.
Ten thousand tasks, one thread each
Suppose 10,000 requests arrive at once, and each one spends its life waiting on a database or a socket. The classic design gives every request a thread from start to finish. On platform threads that design collapses for a boring reason: every request that waits holds an operating system thread while it waits, and the operating system will not hand out 10,000 of them comfortably.
The workarounds of the last decade all attacked the same cost. Reactive pipelines returned their thread to a pool whenever they waited; careful sizing kept the pool to numbers the machine could carry. Both scaled, and both charged a price in debugging, stack traces and plain sequential thinking.
Virtual threads attack the cost itself. Before the API, one question is worth answering: what exactly is expensive about a thread?
// Thread-per-request, the shape people were pushed to abandon: void handle(Request request) throws Exception { User user = users.findById(request.userId()); // blocks on the database Report report = reports.build(user); // more waiting inside respond(request, report); }
The thread was never the scarce resource in these designs. The waiting was.
A platform thread is an OS thread with a Java name
A platform thread is an instance of java.lang.Thread implemented the traditional way, as a thin wrapper around an operating system thread. One Java thread, one OS thread, and the operating system scheduler owns the mapping: which core runs it, when it is preempted, when it resumes.
Everything about that arrangement is kernel policy. A context switch is a kernel operation: registers saved, caches partly cold, the next thread restored. The stack is real memory assigned up front (roughly a megabyte by default on mainstream 64-bit HotSpot builds, tunable with -Xss), so every platform thread is a standing reservation of address space plus kernel bookkeeping.
The ceiling follows from that price list: the number of platform threads is bounded by the operating system, not by your heap. A few thousand is routine. Tens of thousands is where it gets uncomfortable, and 10,000 tasks that each wait for a second is exactly the workload where the discomfort shows.
| Property | Platform thread | What it means in practice |
|---|---|---|
| Scheduling | the OS kernel | the JVM asks; the kernel decides which core and when |
| Mapping | one Java thread per OS thread | creating one is an OS-level operation |
| Stack | roughly 1 MB by default (HotSpot, -Xss) | the bill is paid per thread, whether the stack is used or not |
| Waiting | the OS thread is held | a blocked request keeps its kernel thread for the whole wait |
A platform thread is an OS thread with a Java name, and the OS price list comes with it.
A virtual thread is a task the JVM schedules
A virtual thread is the other half of the idea: an instance of java.lang.Thread that is not tied to any particular OS thread. The JVM schedules it, not the kernel. Many virtual threads (M) multiplex onto a small number of platform threads (N), and the platform thread a virtual thread currently runs on is called its carrier. This is M:N scheduling, the same shape as goroutines and Erlang processes.
A running virtual thread occupies a carrier, exactly as a platform thread occupies a core. The difference is what happens when it blocks. Because the JVM owns the virtual thread, it can park the task and free the carrier for someone else; while parked, the virtual thread's stack lives on as heap data, so nothing kernel-level is held.
The API behind this is deliberately boring: same Thread, same Runnable, same executors, same synchronized and ThreadLocal. What changed is the cost model. When a library needs to know which kind it was handed, Thread.currentThread().isVirtual() answers.
| Question | Platform thread | Virtual thread |
|---|---|---|
| Who schedules it? | the OS kernel | the JVM's scheduler, which mounts it on carriers |
| Mapping | 1:1 with an OS thread | M:N, many virtual threads onto few carriers |
| When it blocks | the OS thread waits alongside it | it unmounts; the carrier takes another task |
| Stack | reserved per thread, roughly 1 MB by default | growable stack chunks in the heap |
| Count | bounded by OS limits | bounded by the heap and by the work being done |
A platform thread is an OS thread with a Java name; a virtual thread is a task the JVM schedules onto one.
mapping · platform threads versus virtual threads
the JVM scheduler decides when a virtual thread rides a carrier1:1 · platform threads
M:N · virtual threads onto carriers
carrier-1
vt-1 mounted
carrier-2
vt-2 mounted
the parked four wait as heap data; a carrier is released the moment a virtual thread blocks
A platform thread is an OS thread with a Java name; a virtual thread is a task the JVM schedules onto one. Mount and unmount happen many times per task, on any free carrier.
Mount, unmount, remount
The unit of progress is the mount. When the scheduler assigns a virtual thread to a carrier, the thread is mounted and its frames are arranged to run on that platform thread. When it blocks on an operation the runtime supports (a socket read, a queue take, Thread.sleep), it unmounts: control returns to the scheduler and the carrier is free for the next runnable task.
When the blocking operation completes, the runtime submits the virtual thread back to the scheduler, which mounts it again, possibly on a different carrier. There is no affinity: yesterday's carrier may be running someone else by now. From inside the task none of this is visible: Thread.currentThread() always returns the virtual thread itself, and the carrier's identity and thread locals are not available to it.
Two scheduler details complete the picture. It is a work-stealing ForkJoinPool whose parallelism defaults to the number of available processors, tunable with the system property jdk.virtualThreadScheduler.parallelism. And it does not implement time sharing: a virtual thread that runs a long CPU loop holds its carrier until it blocks or finishes, unlike the preemptive scheduling the kernel gives platform threads.
- Mounting is cheap: an existing object is assigned to an existing platform thread, with no OS call involved.
- Unmounting is triggered by blocking, not by the scheduler. Virtual threads are not cooperative and will not yield for you.
- Carriers are platform threads, so the number of cores still decides how much CPU-bound work happens at once.
- No affinity, no identity: mount and unmount may happen many times per task, on different carriers, with the carrier invisible to the code.
Nothing about a virtual thread says which carrier runs it, and the JVM reserves that freedom on purpose.
Why cheap threads change the design
Here is the arithmetic that changed. In the JDK's own worked example, 10,000 tasks each sleep for one second. Given platform threads from a pool of 200, the pool finishes at most 200 tasks per second, so the last task completes around the 50 second mark; given virtual threads, all 10,000 sleeps are in flight at once and the whole set finishes in about a second, on the same machine.
Notice what the second number depends on: not the speed of the CPU, but how cheap a waiting thread became. That is the entire point. The design goal of virtual threads is threads in the millions, because what a parked virtual thread consumes is heap, and heap is where the frames it actually built are kept.
The practical consequence is a design that goes back to being obvious: hand every unit of work its own thread, write the sequential code, and let the runtime park the threads while they wait. The tasks that used to need a pool, a queue and a callback no longer need any of the three.
When threads stop being scarce, the queue in front of them stops being the design.
10,000 tasks · one-second waits
model
one OS thread per worker
elapsed 0.0 srunning
200
waiting
9,800
done
0
total
10,000
pool · 200 platform threads
200 mounted, each one holding a task
10,000 tasks submitted to a pool capped at 200 platform threads. 200 start immediately; the other 9,800 sit in the executor queue, each of them costing nothing yet.
outcome · platform model
the thread cap became the throughput cap: about 200 tasks per second
Nothing was slow except the model. Each waiting task held an OS thread, so 200 threads could only ever move 200 tasks per second, and the queue length was the design.
Where the stack lives
The stacks of virtual threads are stored in Java's garbage-collected heap as stack chunk objects, and they grow and shrink as the application runs. A platform thread stack is reserved up front; a virtual thread stack is allocated as frames actually need it, which is why a thread can start tiny and still survive a deep call stack (up to the JVM's configured platform stack size).
Two consequences follow. The memory cost of a parked virtual thread is the frames it genuinely built, not a megabyte of reservation; and virtual thread stacks are not GC roots, so references they hold are not part of a stop-the-world scan by collectors that otherwise scan the heap concurrently.
The honest caveat is that cheap is not free. A virtual thread that runs deep and long still costs real heap, one allocation per continuation chunk while it lives, and the heap is shared with every object in the application. Millions of threads is the design goal, not a promise about your machine.
A parked virtual thread is an object in the heap. That sentence is the whole cost difference.
Always daemon, fixed priority
Virtual threads look like threads and refuse two platform-thread knobs. They are always daemon threads: a virtual thread never keeps the JVM alive, and Thread.setDaemon cannot change that. They also have a fixed priority (NORM_PRIORITY); setPriority has no effect, because the scheduler that would honour priority belongs to the operating system.
Thread groups are a third difference: a virtual thread is not an active member of one, and getThreadGroup() returns the placeholder group named "VirtualThreads". The pattern behind all three facts is the same: the JVM owns the virtual thread's lifetime, so knobs that only made sense to a kernel scheduler simply are not part of the deal.
- Daemon by construction: if
mainreturns while virtual threads are still running, the JVM exits and they die with it. Join them if the work must finish. - Priority is a no-op: the scheduler does not read it, so horse-trading between tasks is not available; design for throughput instead.
- No group to join: observation happens through the newer tooling (
jcmdcan dump virtual threads explicitly), not throughThreadGroup.
The JVM owns the virtual thread, and every knob that belonged to the OS scheduler went with it.
What to remember
Four sentences carry this lesson:
- Platform threads are 1:1 with OS threads: kernel-scheduled, roughly a megabyte of stack each, and tens of thousands is a fight.
- Virtual threads are M:N onto carriers: JVM-scheduled, stack held in the heap, parked instead of blocked.
- Mount and unmount on blocking: a blocked virtual thread steps off its carrier, and the carrier picks up the next task.
- Cheap is the feature: threads stop being the scarce resource, and whatever the tasks wait on becomes the thing to watch.
Virtual threads did not make threads faster; they made waiting cheap enough that the simple design could stay.
The budget was never about threads
Every workaround of the previous decade existed because a waiting thread held an OS thread. Pools capped the damage, callbacks returned threads while they waited, and the code paid for the OS price list either way. Virtual threads remove the price of waiting, not the price of computing: a CPU-bound task still occupies a carrier and still needs a core. Migrate a CPU-heavy service to virtual threads and expect more throughput, and nothing changes except the story you tell about it.
Interview angles
What is the difference between a platform thread and a virtual thread?+
Both are java.lang.Thread. A platform thread is a thin wrapper around an OS thread, scheduled by the kernel. A virtual thread is scheduled by the JVM and mounted onto a platform thread, its carrier, only while it runs.
The mapping differs: platform threads are 1:1 with OS threads, virtual threads are M:N onto carriers. A virtual thread can unmount when it blocks and remount on a different carrier later; the carrier is the thread the OS actually knows about.
Follow-up: What decides how many carriers exist?
What is a carrier, and does a virtual thread keep the same one?+
A carrier is the platform thread the scheduler mounts a virtual thread on. There is no affinity: over one task it may mount and unmount many times, on different carriers.
Common misconception: That the carrier is visible to the task, or that Thread.currentThread() returns it. It returns the virtual thread itself, and the carrier's thread locals are not available to the code running on it.
Are virtual threads faster than platform threads?+
No. They run code no faster and a single task finishes no sooner. In the JEP's own words, they exist to provide scale (higher throughput) rather than speed (lower latency).
A CPU-bound workload gains nothing at all: carriers are platform threads and there are about as many as cores. The JDK example is 10,000 one-second sleeps, which finish in about a second on virtual threads, while a pool of 200 platform threads needs on the order of 50 seconds.
Where does a virtual thread's stack live, and why does that matter?+
In the heap, as garbage-collected stack chunks that grow and shrink with call depth. Stack chunks are not GC roots.
That is what makes a parked virtual thread cheap: it costs the frames it actually built instead of the roughly one megabyte a platform thread stack reserves on default HotSpot settings.
How many virtual threads can you create?+
Enough that the design goal is threads in the millions. The limit stops being the OS thread table and becomes the heap, the number of carriers, and what the tasks actually do.
Common misconception: That there is a tidy number to quote as a limit. The JDK's example programs use 10,000; the stated goal is millions; the useful answer is that thread count leaves the design constraints list.
Why are virtual threads always daemon threads?+
Because their lifetime belongs to the JVM rather than to a kernel thread it asked for. setDaemon cannot change it, and the JVM exits once only daemons remain, so a short program must join its virtual threads.