Java Tutorial

Virtual Threads (JAVA 21+)

High-scale concurrency with lightweight, JVM-managed threads

What are Virtual Threads?

Introduced as a standard feature in JAVA 21, Virtual Threads are the biggest change to Java's concurrency model in over 25 years.
Prior to this, every Java Thread was a "Platform Thread"—a heavy wrapper around an actual thread from the Operating System. Virtual Threads are different. They are lightweight threads managed entirely by the JVM, which makes them incredibly fast and memory-efficient.
java
// Create MILLIONS of threads! (Zero cost!)
Thread t = Thread.ofVirtual().start(() -> {
    System.out.println("Hello from a Virtual Thread!");
});

Comparisons: Platform vs. Virtual Threads

Wait! Why are Virtual Threads better?
FeaturePlatform ThreadVirtual Thread
Cost~1MB memory per thread.~1KB memory.
LimitA few thousands (Crash beyond that!).VIRTUAL MILLIONS.
CreationSlow (Handled by the OS).FAST (Handled by the JVM).
StackFixed size on the OS.Dynamic size on the Heap.

How it Works: "Mounting"

When a Virtual Thread is ready to run, the JVM "mounts" it onto a normal Platform Thread (called a Carrier Thread). If the Virtual Thread hits a blocking task (like a file read or network request), the JVM "unmounts" it!
The Carrier Thread is now free to run a DIFFERENT Virtual Thread. This is how Java can handle millions of tasks with only a few dozen real CPU threads.

Why Use Virtual Threads?

  1. Massive Scale: Perfect for web servers handling 100,000+ simultaneous requests (like a chat app or microservice).
  2. Simple Code: No more complex, "non-blocking" async code! You can write simple, synchronous-looking code that is actually highly scalable.
  3. Efficiency: Zero cost for switching between tasks.

Summary

TermDescription
VirtualLightweight, JVM-managed thread (~1KB)
CarrierThe real Platform/OS thread that RUNS the virtual one
BlockingWhen a thread waits for data; virtual threads simply "unmount"
MountAttaching a virtual task to a real CPU thread
Best ForHigh-concurrency servers, microservices, huge background jobs
Java 21The first standard version with Virtual Threads!
Project L."Project Loom" is the original name of this feature
SafePerfectly compatible with all existing synchronized code!
ExecutornewVirtualThreadPerTaskExecutor() (The ultimate pool!)
SimpleWrite normal code; get async performance!
StackVirtual stacks live in the Java "Heap" memory
CreationThread.ofVirtual().start(Runnable)
StandardEstablished and trusted worldwide for 25+ years
LimitOnly limited by your available RAM!
StrategyStop using complex "Reactive/Async" libraries; use Virtual!
SafetyNo risk of "Thread Starvation" in high-load situations
ManagedThe JVM automatically handles all mounting logic
CarrierA small pool of Platform threads handles all virtual work
ContextSwitching virtual threads is 100x faster than OS threads
ScaleOne server can now handle what five servers used to!
ModernThe future of all high-performance Java development
ChoiceContinue using Platform threads for heavy CPU-bound math
RefactorReplacing standard pools with Virtual Thread executors
FutureFrameworks like Spring Boot are now built on Virtual Threads!