Java core learning path Level 2 This level · 6 of 29
Medium java java-21

How do virtual threads differ from platform threads?

The 30 second answer

A platform thread is a thin wrapper over one OS thread for its whole life. It's expensive, so we pool them. A virtual thread is cheap: you make one per task and never pool it. They don't make code faster; they let many more tasks wait at once, so they help throughput for I/O-heavy work, not CPU-heavy work. In Java 21, watch out for pinning inside synchronized blocks.

Could you say this in an interview?
Platform threadVirtual thread
Backed byone OS thread for its lifetimemounted on a carrier only while running
Costlarge OS stack, OS schedulingsmall heap-allocated stack, JDK scheduling
Typical countthousands at mostcan be millions
Poolingyesno, one per task
Good forCPU-bound work, long-lived threadsmany concurrent blocking tasks

Same job, both ways (runnable, Java 21+). Each task blocks for 100 ms, like a remote call. The platform version needs a bounded pool because every thread is an OS thread; the virtual version starts one thread per task.

import java.time.Duration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

public class VirtualVsPlatform {
    static void blockingCall() {               // stands in for an HTTP or DB call
        try { Thread.sleep(Duration.ofMillis(100)); }
        catch (InterruptedException e) { Thread.currentThread().interrupt(); }
    }

    static long run(ExecutorService executor, int tasks) {
        long start = System.nanoTime();
        try (executor) {                       // close() waits for all tasks
            IntStream.range(0, tasks).forEach(i -> executor.submit(VirtualVsPlatform::blockingCall));
        }
        return Duration.ofNanos(System.nanoTime() - start).toMillis();
    }

    public static void main(String[] args) {
        int tasks = 10_000;
        // Platform threads: a bounded pool, because each thread is an OS thread
        long platformMs = run(Executors.newFixedThreadPool(200), tasks);
        // Virtual threads: one new thread per task, no pool
        long virtualMs = run(Executors.newVirtualThreadPerTaskExecutor(), tasks);
        System.out.println("platform pool of 200: " + platformMs + " ms");
        System.out.println("virtual per task:     " + virtualMs + " ms");
    }
}

What it shows: with 10,000 tasks of 100 ms and 200 pool threads, the platform pool needs at least 10,000 / 200 × 100 ms = 5 seconds, because only 200 tasks can wait at once. The virtual threads all wait at the same time, so the run takes little more than one 100 ms wait. The code inside each task is the same blocking code; only the executor changes. If the tasks were CPU-bound instead of sleeping, the virtual version would not be faster.

Pinning in Java 21: a virtual thread is pinned to its carrier when it blocks inside a synchronized block or method, or during a native call. A pinned thread keeps the carrier busy, which can hurt throughput. JEP 444 suggests replacing synchronized with ReentrantLock around frequent, long blocking operations, and -Djdk.tracePinnedThreads helps find them. Java 24 (JEP 491) removed the synchronized pinning case; native calls can still pin.

Other notes: don't pool virtual threads to limit concurrency (use a Semaphore), and be careful with large ThreadLocal caches, since there may be very many threads.

Sources (3)
  1. JEP 444: Virtual Threadsopenjdk.org
  2. JEP 491: Synchronize Virtual Threads without Pinningopenjdk.org
  3. Executors Javadoc (Java 21): newFixedThreadPool and newVirtualThreadPerTaskExecutordocs.oracle.com
Previous
Next