6

Writing simpler reactive REST services with Quarkus Virtual Thread support

 1 year ago
source link: https://quarkus.io/guides/virtual-threads
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

What are virtual threads ?

Terminology

OS thread

A "thread-like" data-structure managed by the Operating System.

Platform thread

Up until Java 19, every instance of the Thread class was a platform thread, that is, a wrapper around an OS thread. Creating a platform threads creates an OS thread, blocking a platform thread blocks an OS thread.

Virtual thread

Lightweight, JVM-managed threads. They extend the Thread class but are not tied to one specific OS thread. Thus, scheduling virtual threads is the responsibility of the JVM.

Carrier thread

A platform thread used to execute a virtual thread is called a carrier. This isn’t a class distinct from Thread or VirtualThread but rather a functional denomination.

Differences between virtual threads and platform threads

We will give a brief overview of the topic here, please refer to the JEP 425 for more information.

Virtual threads are a feature available since Java 19 aiming at providing a cheap alternative to platform threads for I/O-bound workloads.

Until now, platform threads were the concurrency unit of the JVM. They are a wrapper over OS structures. This means that creating a Java platform thread actually results in creating a "thread-like" structure in your operating system.

Virtual threads on the other hand are managed by the JVM. In order to be executed, they need to be mounted on a platform thread (which acts as a carrier to that virtual thread). As such, they have been designed to offer the following characteristics:

Lightweight

Virtual threads occupy less space than platform threads in memory. Hence, it becomes possible to use more virtual threads than platform threads simultaneously without blowing up the heap. By default, platform threads are created with a stack of about 1 MB where virtual threads stack is "pay-as-you-go". You can find these numbers along with other motivations for virtual threads in this presentation given by the lead developer of project Loom: https://youtu.be/lIq-x_iI-kc?t=543.

Cheap to create

Creating a platform thread in Java takes time. Currently, techniques such as pooling where threads are created once then reused are strongly encouraged to minimize the time lost in starting them (as well as limiting the maximum number of threads to keep memory consumption low). Virtual threads are supposed to be disposable entities that we create when we need them, it is discouraged to pool them or to reuse them for different tasks.

Cheap to block

When performing blocking I/O, the underlying OS thread wrapped by the Java platform thread is put in a wait queue and a context switch occurs to load a new thread context onto the CPU core. This operation takes time. Since virtual threads are managed by the JVM, no underlying OS thread is blocked when they perform a blocking operation. Their state is simply stored in the heap and another Virtual thread is executed on the same Java platform thread.

Virtual threads are useful for I/O-bound workloads only

We now know that we can create way more virtual threads than platform threads. One could be tempted to use virtual threads to perform long computations (CPU-bound workload). This is useless if not counterproductive. CPU-bound doesn’t consist in quickly swapping threads while they need to wait for the completion of an I/O but in leaving them attached to a CPU-core to actually compute something. In this scenario, it is useless to have thousands of threads if we have tens of CPU-cores, virtual threads won’t enhance the performance of CPU-bound workloads.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK