Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is a virtual thread in Java 21+, how do you create one, and how does it differ from a platform thread?

A virtual thread is an ordinary java.lang.Thread that the JVM schedules itself, running it on a small pool of platform threads instead of giving it its own OS thread. We create one with Thread.ofVirtual().start(task), Thread.startVirtualThread(task) or Executors.newVirtualThreadPerTaskExecutor(). They are cheap enough to have millions, because their stacks live on the heap and grow as needed, and when one blocks on a supported operation it unmounts and frees its carrier thread. Virtual threads are always daemon threads with normal priority, and they should not be pooled.

Why this question is asked

Virtual threads, final since Java 21, change how we write servers. For years we had to choose between simple blocking code that needed a thread per request and scaled poorly, and asynchronous code that scaled well but was hard to read and debug. Virtual threads let us keep the blocking style and still handle tens of thousands of concurrent requests. Interviewers ask about them to see whether we know what they are, how to create them, and how they differ from the platform threads we have used until now. In this answer, we will recap platform threads, see what a virtual thread is, create them in three ways, look at why they are cheap and what happens when they block, and finish with their fixed properties and why we do not pool them.

1A Quick Recap of Platform Threads

Before Java 21, every java.lang.Thread was what we now call a platform thread: a thin wrapper around an operating system thread. The OS schedules it, it has a fixed-size stack reserved up front (often 1 MB by default on 64-bit Linux, set with -Xss), and creating it takes a system call. A server can have a few thousand of them before memory and scheduling overhead become a problem.

That limit shaped how servers were built. A thread-per-request server like Tomcat used a pool of about 200 threads, and when every thread was blocked waiting for a database or an HTTP call, new requests had to wait, even though the CPU was idle.

2What a Virtual Thread Is

A virtual thread is also an instance of java.lang.Thread, with the same API, but it is not tied to an OS thread. The JVM runs it on a carrier, which is a platform thread from a small pool owned by the virtual thread scheduler, by default a ForkJoinPool with about as many threads as CPU cores. While the virtual thread runs, it is mounted on that carrier. When it blocks on an operation the JDK supports, such as a socket read, a sleep or waiting on a lock, it unmounts: its state is saved, and the carrier is free to run another virtual thread. When the operation can continue, the virtual thread is scheduled again, possibly on a different carrier.

So many virtual threads share few OS threads, and blocking no longer wastes an OS thread. That is the whole idea.

Platform and virtual threads
AspectPlatform threadVirtual thread
Scheduled bythe operating systemthe JVM, on carrier threads
Stackfixed size reserved up front, about 1 MBon the heap, grows and shrinks as needed
Cost to createsystem call, expensivean object allocation, cheap
Practical countthousandsmillions
When it blocksholds its OS threadunmounts and frees the carrier
Daemon and priorityconfigurablealways daemon, always normal priority
Poolingcommondo not pool; one per task

3Three Ways to Create One

The API is the same Thread class with a new builder and a few helpers.

Java
Thread t1 = Thread.ofVirtual().name("worker-1").start(() -> work()); // builder, can set a name
Thread t2 = Thread.startVirtualThread(() -> work()); // shortest form
System.out.println(t1.isVirtual()); // true
try (ExecutorService ex = Executors.newVirtualThreadPerTaskExecutor()) {
Future<String> f = ex.submit(() -> fetch("https://example.com")); // a new virtual thread per task
System.out.println(f.get().length()); // 1256, say
} // close() waits for submitted tasks
ThreadFactory factory = Thread.ofVirtual().name("job-", 0).factory(); // job-0, job-1, ... for frameworks

Notice that the executor does not have a pool size. It creates a new virtual thread for every task it receives, which is how virtual threads are meant to be used. In Spring Boot 3.2 and later, setting spring.threads.virtual.enabled=true makes Tomcat, @Async and several other components run their work on virtual threads without any code change.

4Why They Are Cheap

A platform thread's cost is mostly its OS resources: a large reserved stack and a kernel scheduling entry. A virtual thread has neither. Its stack is stored on the Java heap as a chain of small objects, which starts small and grows only as deep as the code actually goes. A virtual thread waiting for a socket with a shallow stack might use a few hundred bytes to a few kilobytes. Creating one is roughly the cost of allocating an object, so creating a million is realistic.

Java
Instant start = Instant.now();
try (var ex = Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < 1_000_000; i++) ex.submit(() -> { Thread.sleep(1000); return null; });
}
System.out.println(Duration.between(start, Instant.now()).toSeconds()); // about 2 to 4 on a laptop
// the same loop with platform threads usually fails with OutOfMemoryError: unable to create native thread

The point of the example is not that each task got faster. Each task still sleeps a full second. What changed is that a million tasks can wait at the same time, so the throughput of waiting work goes up. Virtual threads help when tasks spend most of their time blocked on I/O; they do nothing for CPU-bound work, which is still limited by the number of cores.

5Fixed Properties and Why We Do Not Pool Them

Some things are fixed for virtual threads. They are always daemon threads, so they do not keep the JVM alive; calling setDaemon(false) throws IllegalArgumentException. Their priority is always Thread.NORM_PRIORITY, and setPriority() has no effect. They belong to a single special thread group.

We should not put virtual threads in a pool. Pools exist to reuse expensive threads and to cap how many exist. Virtual threads are not expensive, so reuse saves nothing, and a pool would bring back the problems we wanted to escape, such as tasks waiting for a free thread. If we need to cap concurrency, for example to protect a database, the right tool is a Semaphore around that specific resource, not a pool of threads.

6Follow-up questions

Does Thread.currentThread() return the carrier or the virtual thread?

It returns the virtual thread. The carrier is an implementation detail, invisible to our code. That is why existing code, including code that uses ThreadLocal, keeps working: each virtual thread has its own thread-locals.

Do virtual threads make code faster?

Not each task. A request that takes 80 milliseconds still takes about 80 milliseconds. They let a server run many more such requests at once with the same hardware, which raises throughput for I/O-bound work and keeps latency stable under load.

Are there operations that do not unmount?

Yes. Some operations capture the carrier instead, and before JDK 24 blocking inside synchronized pinned the virtual thread to its carrier. JEP 491 in JDK 24 removed most of that; native calls on the stack can still pin. Those cases matter when choosing a JDK version for production.

7Conclusion

In this answer, we saw that a virtual thread is a java.lang.Thread scheduled by the JVM on a small pool of carrier threads rather than mapped one-to-one to an OS thread. We created them with Thread.ofVirtual().start(), Thread.startVirtualThread() and Executors.newVirtualThreadPerTaskExecutor(). They are cheap because their stacks live on the heap and grow as needed, so millions can exist, and when one blocks on a supported operation it unmounts and frees its carrier. They are always daemon threads with normal priority, and we create one per task instead of pooling them.

What to say in the interviewAbout 80 seconds aloud

Open with

A virtual thread is still a java.lang.Thread, same API, but instead of being mapped one-to-one to an OS thread, the JVM schedules it onto a small pool of platform threads called carriers, by default a ForkJoinPool with about one thread per core.

Then, if they want more

You create one with Thread.ofVirtual().start, or Thread.startVirtualThread, or most commonly Executors.newVirtualThreadPerTaskExecutor, which makes a new virtual thread for every task. In Spring Boot you can just set spring.threads.virtual.enabled. The difference from a platform thread is cost and blocking behaviour. A platform thread reserves a big fixed stack and is scheduled by the OS, so you can have thousands.

A virtual thread's stack lives on the heap and grows as needed, so you can have millions. And when a virtual thread blocks on something the JDK supports, like a socket read or a sleep, it unmounts, its stack is saved, and the carrier goes off to run another virtual thread.

So blocking no longer wastes an OS thread, which is great for I/O-bound servers, though it doesn't speed up CPU-bound work. A few fixed properties: they're always daemon threads, always normal priority, and you shouldn't pool them, since they're cheap. If you need to limit concurrency, use a semaphore.

About this answer

Last updated
12 November 2026
Versions it names
Java 21, Java 24, Spring Boot 3.2
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
JEP 444, Virtual Threads
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is a virtual thread in Java 21+, how do you create one, and how does it differ from...": https://scrivio-interview-library.netlify.app/java/virtual-threads/a4b8f9005c/