Virtual threads

A platform thread is a thread of the operating system, and the machine has a few thousand of them at most. A virtual thread is a thread the JVM schedules itself: it has a stack that grows on the heap, it costs a few hundred bytes when it is parked, and a program can have a million of them. Java 21 has them as a final feature, and this lesson measures the difference.

Thread.ofVirtual

Thread.ofVirtual() is the builder, and it takes a name the same way the platform builder does. isVirtual answers which kind a thread is, for the thread itself and for a reference to it:

public class Virtual { public static void main(String[] args) throws InterruptedException { Thread virtual = Thread.ofVirtual().name("v-1").start(() -> { System.out.println("running on " + Thread.currentThread().getName()); System.out.println("is virtual: " + Thread.currentThread().isVirtual()); }); virtual.join(); Thread platform = new Thread(() -> { System.out.println("platform is virtual: " + Thread.currentThread().isVirtual()); }, "p-1"); platform.start(); platform.join(); System.out.println("main is virtual: " + Thread.currentThread().isVirtual()); } }

javac -Xlint:all Virtual.java
java Virtual

running on v-1
is virtual: true
platform is virtual: false
main is virtual: false

The API is the same Thread class, and that is deliberate: a method that takes a Runnable or a Thread does not change, and neither does the code inside it. The main thread of a program is a platform thread, and it stays one.

Ten thousand tasks

Executors.newVirtualThreadPerTaskExecutor makes one virtual thread per task, which is the opposite of a pool and is what the class is for: a thread is now cheap enough that reusing it is the wrong optimisation. The program runs the same ten thousand sleeping tasks through a pool of a hundred platform threads and through the virtual executor:

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class Many { static final int TASKS = 10_000; static final int SLEEP = 10; public static void main(String[] args) throws InterruptedException { long platform = run(Executors.newFixedThreadPool(100)); long virtual = run(Executors.newVirtualThreadPerTaskExecutor()); System.out.println("tasks: " + TASKS + " sleeping " + SLEEP + " ms"); System.out.printf("platform pool: %d ms%n", platform); System.out.printf("virtual threads: %d ms%n", virtual); System.out.println("virtual faster: " + (virtual < platform)); } static long run(ExecutorService pool) throws InterruptedException { long start = System.nanoTime(); for (int i = 0; i < TASKS; i++) { pool.submit(() -> { try { Thread.sleep(SLEEP); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }); } pool.shutdown(); pool.awaitTermination(30, TimeUnit.SECONDS); return (System.nanoTime() - start) / 1_000_000; } }

javac -Xlint:all Many.java
java Many

tasks: 10000 sleeping 10 ms
platform pool: 1066 ms
virtual threads: 244 ms
virtual faster: true

The measurement

The platform pool has a hundred threads, so ten thousand sleeping tasks run in a hundred batches of ten milliseconds and the floor is about a second. The virtual executor starts ten thousand threads, each sleeps ten milliseconds at the same time, and the run is a quarter of a second. The numbers are one container's and they move with the machine, so the program prints the boolean as well: the direction is what the lesson claims.

The virtual time is not zero because the tasks themselves have to be created and the JVM has to run them; the point is that the sleep does not hold a thread of the operating system, so ten thousand sleeps cost about one. A pool is a workaround for the cost of a platform thread, and with virtual threads the workaround is unnecessary: newVirtualThreadPerTaskExecutor is the default executor a server should use.

What still blocks a virtual thread

A virtual thread is cheap because it can be parked off its carrier thread, and it cannot be parked while it holds a lock it took with synchronized: it then pins its carrier, and the carrier cannot run other virtual threads until the block ends. The tools report it (-Djdk.tracePinnedThreads=full), and the fix is a ReentrantLock of lesson 119, which lets the thread park.

The other pinning case is a native method or a foreign call, which a virtual thread cannot leave. Neither appears in the programs of this course, and both are the reason a server that uses virtual threads is checked with the pinned-thread trace before it is trusted with a load.

When to use them

Use a virtual thread for work that spends its time waiting: a request that calls a database, a task that reads a file, a job that waits for a reply. Use a platform thread, or a pool of them, for work that computes: a video encode, a number crunch, a task that never blocks, because a core is a core and a million virtual threads do not make more of them.

The thread-per-task model comes back with them. Instead of a pool and a queue, a server accepts a connection and starts a virtual thread for it, which is the code a person writes first and the code that scales now. The structured concurrency API that groups such tasks is a preview feature in 21, so this course does not use it.

Exercises

  1. Start a million virtual threads that each increment a LongAdder and print the sum.
  2. Run Many with a pool of ten platform threads and compare the time with the hundred.
  3. Put a synchronized block with a sleep inside a virtual thread and run with -Djdk.tracePinnedThreads=full.
  4. Use Thread.ofPlatform() for the second program and describe what changes.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is a virtual thread?

    Answer

    A thread the JVM schedules, with a stack on the heap

    The scheduler is the JVM and the stack grows on the heap, which is what makes it cheap.

  2. Why is newVirtualThreadPerTaskExecutor not a pool?

    Answer

    It makes a thread per task, because a virtual thread is cheap enough

    Reuse is the point of a pool, and a virtual thread costs little enough to skip it.

  3. What happens when a virtual thread blocks inside a synchronized block?

    Answer

    It pins its carrier thread, which cannot run another virtual thread

    The lock is held by the carrier, so the virtual thread cannot leave it; ReentrantLock is the fix.

  4. What does this program print? public class Q1 { public static void main(String[] args) throws InterruptedException { Thread v = Thread.ofVirtual().start(() -> System.out.println("virtual")); v.join(); System.out.println(v.isVirtual()); System.out.println(Thread.currentThread().isVirtual()); } }

    Answer

    virtual / true / false

    The started thread is virtual and main is a platform thread.

  5. For which work is a platform thread still the right choice?

    Answer

    Work that computes and never blocks

    A virtual thread frees its carrier only while it waits, and a computation occupies the core either way.

  6. What does isVirtual answer on the main thread?

    Answer

    false, because main is a platform thread

    The thread that starts a program is a platform thread, and the lesson prints false.

  7. Why is the thread-per-task model practical again?

    Answer

    A virtual thread is cheap enough that one per task is affordable

    The pool existed to hide the cost of a platform thread, and the virtual thread removes that cost.

  8. What does this program print? public class Q2 { public static void main(String[] args) throws InterruptedException { Thread a = Thread.ofVirtual().name("a").unstarted(() -> {}); System.out.println(a.getName()); a.start(); a.join(); System.out.println(a.isVirtual()); } }

    Answer

    a / true

    The builder takes the name, and the thread it made is virtual.

  9. What does the pinned-thread trace show?

    Answer

    The virtual threads that block while holding a monitor

    A thread that cannot park holds its carrier, and the trace names it.

  10. Which executor is the default for a server that waits on many calls?

    Answer

    newVirtualThreadPerTaskExecutor

    Every task gets a thread, and the waiting costs no carrier thread while it lasts.

The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.

Article author: Arthur Isaev

Banner image