Your JVM Just Learned to Juggle, with Virtual Threads
If your computer could do only ten things at once, you'd probably throw it away pretty quickly. Luckily, it can do more like a hundred thousand things at once.
What does this have to do with the JVM? (follow thread --> )
Your OS is like a juggler who uses their hands (physical threads) to keep many balls (logical threads) in motion. If a ball gets sticky (operation is blocking/waiting), the juggler sets it aside. When the ball isn't sticky anymore, the juggler launches it into motion once again.
In other words, this juggler—the OS Thread Scheduler—unmounts logical threads from physical threads when they're blocking. No sense wasting precious hand-time twiddling thumbs.
What About Java Threads?
Java creates its own Threads and attaches them to OS logical threads. When your Java code blocks, the "ball" is set aside, and your Java thread gets stuck with it, waiting. That limits the amount of work your Java process can handle.
A New Juggler In the House!
Introducing the new Java 21* Thread Scheduler! This JVM Juggler uses traditional Java "platform" threads as hands, and deftly juggles a whole new class of super-light, super-fast VirtualThread "balls."
* Experimentally available in earlier versions.
Virtual threads are so light & fast the JVM can create them on-demand and discard them. Micro vs millisecond start. Kilo vs megabyte memory. 1000x faster and smaller! This means the JVM can start MILLIONS of them. And no need for fancy tricks like thread pooling!
Bye Bye Hardware Tuning
The JDK Scheduler configures the number of Java platform threads according to your system's physical threads, automatically. This means that with virtual threads you get better hardware utilization without tuning, even as you resize or change hardware.
The end result:
For imperative-style applications with concurrent blocking or a scatter-gather pattern, you'll get better performance with a smaller footprint.
Virtual threads give you the illusion of nearly infinite concurrency, and hardware utilization is near optimal.
Spring Boot Easy
Spring Boot 3.2 and Java 21 make it easy to take advantage of Virtual Threads. Just set:
spring.threads.virtual.enabled=true
As an example, if you're using Web MVC, this setting tells Spring Boot to configure Tomcat to use Virtual Threads. Easy peasy.
A Picture Is Worth A Thousand Words
The Java Scheduler and Java Virtual Threads are new and official in Java 21.
Spring Boot 3.2 with `spring.threads.virtual.enabled=true` makes it a cinch to use them.
Keep in mind:
- This applies to imperative-style code bases, like Web MVC. Reactive code bases use asynchronous thread processing to optimize performance.
- There's no silver bullet: if your app is CPU-intensive, you may be better off sticking with traditional thread pools.
0
Thread