What you will learn
5- Explain what a virtual thread is and who schedules it.
- Describe how a virtual thread differs from a platform thread.
- Recognise the workloads that gain from virtual threads.
- Recognise the workloads that gain nothing and the risks that remain.
- Apply a short test and a measurement step before you adopt them.
Virtual threads (the result of Project Loom) let you keep simple, blocking-style Java code and still serve a very large number of concurrent tasks. They are not a speed-up for every program. This article explains the idea from first principles, then shows where it pays off and where it does not.
Each section answers one question. The article contains no installation steps and no version-specific API listing: check the official Java documentation for your Java release when you write the code.
What is a virtual thread?
A virtual thread is a lightweight thread that the Java runtime manages, not the operating system. It is a user-mode thread: the JVM decides when it runs, when it waits and when it resumes.
Your code does not change shape. A virtual thread runs top to bottom, calls methods and blocks on I/O like any other thread. The difference is entirely underneath, in what the thread costs while it waits.
Project Loom is the name of the effort that introduced them. You will see that name in older articles and talks on the topic.
How is it different from a platform thread?
A traditional platform thread has a 1:1 mapping to an operating system (OS) thread. Each one you create holds an OS thread for its whole life, even while it only waits. OS threads are a limited resource, so the number you can afford is limited too.
A virtual thread is a task the JVM parks and resumes on demand. It borrows an OS thread only while it is actually running. When it has nothing to do, it holds no OS thread.
| Platform thread | Virtual thread | |
|---|---|---|
| Managed by | Operating system | JVM |
| Mapping to OS thread | 1:1 for its whole life | Borrowed only while running |
| Cost of waiting | Holds an OS thread | Parked, holds none |
| Practical count | Limited by OS threads | Very large numbers of tasks |
Read the last row as a consequence of the first three, not as a promise. How many you can run depends on memory and on what the tasks do.
Why does blocking become cheap?
Most server code spends its time waiting: for a database, a remote service or a file. With a platform thread, every wait keeps an OS thread idle. To serve more concurrent requests you need more OS threads, and you run out.
With a virtual thread, a wait is different. The JVM parks the virtual thread and the OS thread it borrowed is free to run another virtual thread. When the wait ends, the JVM resumes the parked thread on an OS thread.
The result is that you can write plain sequential code, one task per virtual thread, and the runtime does the multiplexing. You do not need callbacks or chained asynchronous stages to avoid tying up threads.
Which workloads fit virtual threads?
Virtual threads suit tasks that are mostly blocked, usually waiting for I/O to complete. Three shapes show up again and again:
- Request per task. A handler that calls a database and two downstream services, then builds a response. Almost all of its elapsed time is waiting.
- Fan-out. One request that starts thousands of independent remote calls and collects the results.
- Many idle connections. A service that holds a large number of connections where each one is usually quiet.
Take an illustrative handler that needs 200 ms end to end, of which about 190 ms is waiting on other systems. A platform thread is occupied for all 200 ms. A virtual thread uses an OS thread for roughly the 10 ms of actual work. The same pool of OS threads can therefore carry far more of these tasks at once.
The common thread is high concurrency, mostly waiting and simple code. If your service matches all three, virtual threads are a strong candidate.
When should you not use virtual threads?
Skip or delay them in these cases.
CPU-bound work. A virtual thread borrows an OS thread while it runs. A task that computes the whole time never waits, so it is never parked and never gives the OS thread back. You gain nothing, and the number of cores still caps your parallelism.
Few concurrent tasks. If you run a handful of long-lived tasks, platform threads already handle that. Virtual threads solve a scale problem you do not have.
Code that depends on a scarce resource. Virtual threads make it easy to start a huge number of tasks. A database or a remote service that can only handle a limited load will still be overwhelmed if you let every task call it at once. You still need to limit concurrency at that resource. Check the official Java documentation for the recommended way to do this in your release.
Code you cannot test under load. Your own libraries, locking and native calls can change how a virtual thread behaves in practice. Before you migrate production code, read the official Java documentation for known limitations and test with realistic concurrency.
Virtual threads or reactive code?
Both approaches address the same problem: how to serve many concurrent I/O-bound tasks without one OS thread per task.
Reactive code solves it by never blocking. You describe the work as an asynchronous pipeline. Virtual threads solve it by making blocking cheap, so you keep sequential code that is easier to read, debug and step through.
Pick virtual threads when your goal is high I/O concurrency with the least change to how the code reads. If you already run a working reactive pipeline, the argument for a rewrite is not made by this article alone. Measure first, and rewrite only when the numbers justify the cost.
How do you decide for your own service?
Answer these four questions in order.
- Does the service run many tasks at the same time? If not, stop: platform threads are enough.
- Is each task mostly waiting on I/O? If not, stop: you will gain little.
- Can you keep a limit on the downstream resources? If not, add one first.
- Can you test the change under realistic load? If not, do not ship it yet.
If all four are yes, run the same workload with platform threads and with virtual threads. Compare throughput and latency at the concurrency you expect in production, not at a toy level. Trust the measurement over any rule of thumb, including the ones above.
How do you create a virtual thread in code?
The exact class and method names depend on your Java release, so this article does not show code. Look up the current API in the official Java documentation on docs.oracle.com for your release and copy the names from there.
When you do, keep the shape of your code the same: one task, written in a blocking style, per virtual thread. Do not wrap virtual threads in a fixed-size pool built for platform threads, since the point is that each task gets its own cheap thread. Confirm the recommended pattern in the documentation for your release.
Glossary
- Virtual thread
- A lightweight, user-mode thread managed by the JVM. It is parked when it waits and resumed on demand, and it borrows an OS thread only while running.
- Platform thread
- A traditional Java thread with a 1:1 mapping to an operating system thread.
- OS thread
- A thread scheduled by the operating system. It is a limited resource.
- Parking
- The JVM suspending a virtual thread while it waits, so that its OS thread can run other work.
- Project Loom
- The name of the effort that introduced virtual threads to Java.
- I/O-bound task
- A task that spends most of its time waiting for input or output to complete, such as a network or database call.
Frequently asked questions
Are virtual threads faster than platform threads?
Not for a single task. They help when you have a very large number of tasks that mostly wait on I/O, because waiting no longer holds an OS thread. For CPU-bound work they give no gain.
Does a virtual thread still use an OS thread?
Yes, but only while it is actually running. When it waits, the JVM parks it and the OS thread is free for other virtual threads.
Do I have to change my code to use virtual threads?
You keep the same blocking, sequential style. What changes is how you create the threads, so check the official Java documentation for your release for the API.
Should I replace reactive code with virtual threads?
Not automatically. Both address high I/O concurrency. Virtual threads keep code sequential, but a working reactive pipeline should be replaced only when measurements justify it.
Can I run unlimited tasks with virtual threads?
Starting many tasks is cheap, but the systems they call are not unlimited. Keep a limit on concurrent calls to scarce resources such as a database.
Next steps
- Read the official Java documentation on docs.oracle.com for the virtual thread API in your Java release.
- Run a load test of one real endpoint with platform threads and with virtual threads, and compare throughput and latency.
- List the downstream resources your service calls and decide how you will limit concurrency to each.
References
From Grokem: https://www.grokem.com/programming/web-development/backend/java-virtual-threads-explained-which-workloads-gain-and-which-do-not







