User Avatar

Cora Iberkleid 🚢

🍃Spring

1y ago

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 --> )

1/11

User Avatar

Cora Iberkleid 🚢

1y ago

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.

2/11

User Avatar

Cora Iberkleid 🚢

1y ago

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.

3/11

User Avatar

Cora Iberkleid 🚢

1y ago

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.

4/11

User Avatar

Cora Iberkleid 🚢

1y ago

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.

5/11

User Avatar

Cora Iberkleid 🚢

1y ago

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!

6/11

User Avatar

Cora Iberkleid 🚢

1y ago

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.

7/11

User Avatar

Cora Iberkleid 🚢

1y ago

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.

8/11

User Avatar

Cora Iberkleid 🚢

1y ago

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.

9/11

User Avatar

Cora Iberkleid 🚢

1y ago

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.

10/11

User Avatar

Cora Iberkleid 🚢

1y ago

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.

11/11

The all-in-one writing platform.

Write, publish everywhere, see what works, and become a better writer - all in one place.

Trusted by 80,000+ writers