Java 21 Runtime Architecture
SummaryJava 21 introduces Virtual Threads for high-throughput concurrency,...
Java 21 introduces Virtual Threads for high-throughput concurrency,...
Java 21 introduces Virtual Threads for high-throughput concurrency, offering significant improvements over traditional platform threads in memory footprint and context switching efficiency
Java 21 Runtime Architecture
Introduction
Java 21 is the first Long-Term Support (LTS) release to include Virtual Threads as a permanent feature, solving the scalability bottleneck where OS threads are exhausted before CPU or Network resources [1]. Virtual threads are designed specifically for ‘thread-per-request’ architectures and blocking I/O tasks, making them an ideal choice for high-throughput concurrency.
Virtual Threads vs Platform Threads
Traditional platform threads typically allocate ~1MB of memory for their stack, whereas virtual threads consume significantly less (bytes to kilobytes initially) [2]. This significant reduction in memory footprint allows for a much higher number of concurrent threads, making virtual threads suitable for applications that require a large number of threads.
Comparison of Platform Threads and Virtual Threads
The following table compares the features of platform threads and virtual threads:
| Feature | Platform Threads (Traditional) | Virtual Threads (Loom) |
|---|---|---|
| Creation Cost | High (OS call) | Low (JVM Object) |
| Memory Footprint | ~1 MB per stack | ~KB (dynamic) |
| Context Switching | OS Kernel (Expensive) | JVM Scheduler (Cheap) |
| Max Concurrent | Thousands (Limited by OS) | Millions (Limited by RAM) |
| Blocking I/O | Blocks OS Thread (Wasteful) | Unmounts VT (Efficient) |
Structured Concurrency
Structured concurrency is a programming model that treats a group of related tasks as a single unit of work, ensuring all subtasks complete or cancel together [3]. This model is particularly useful when working with virtual threads, as it provides a way to manage the complexity of concurrent programming.
Example of Structured Concurrency
The following code example demonstrates the use of structured concurrency with virtual threads:
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
IntStream.range(0, 10_000).forEach(i -> {
executor.submit(() -> {
// Simulated blocking I/O call
Thread.sleep(Duration.ofSeconds(1));
return i;
});
});
} // Executor automatically waits for all tasks to finish on close
Conclusion
In conclusion, Java 21’s virtual threads and structured concurrency provide a powerful combination for achieving high-throughput concurrency. By understanding the differences between platform threads and virtual threads, and by using structured concurrency to manage complexity, developers can create efficient and scalable applications.
Sources
[1] https://www.games24x7.com/resource/virtual-threads-fast-furious-and-sometimes-stuck [2] https://www.sobyte.net/post/2022-09/java19-vt/ [3] https://www.javacodegeeks.com/2025/11/structured-concurrency-patterns-in-java.html