Virtual Threads: New Platform for Large-scale Java Applications

Tram Ho

Virtual threads is an ambitious project on the Java platform, which is expected to significantly improve high-load applications and is one of the major changes of the JVM architecture after a long time. Expectations with virtual threads in Java 19 will be covered in this article.

One of the notable updates of Java 19 is the introduction of virtual threads. This is considered a new platform for large-scale Java applications, promising to improve performance for large Java-based systems, especially systems that need a lot of parallel processing.

Quick explanation of Thread and Virtual Thread

In computer architecture, a thread is a child of a process. Threads are created and managed by the operating system.

In contrast, a virtual thread is a thread that is created and managed by a platform instead of being created and managed by the operating system like a regular thread. The best example of a virtual thread is Golang’s goroutine.

Recently Java has also provided the ability to create virtual threads using JEP 425 in JDK 19 with the Project Loom project code.

Because virtual threads are created by the platform, the platform can theoretically create an almost infinite number of virtual threads, not limited by the number of threads of the operating system. In addition, creating a virtual thread also consumes many times less system resources than a regular thread, making it easier to create and manage virtual threads.

The following table compares the differences between threads and virtual threads:

ThreadsVirtual Threads
Created and managed by the operating systemCreated and managed by platform
Consumes a lot of resourcesConsume less resources
Difficult to manageEasy to manage
A fewGreater numbers

 

The next section will learn how to use the virtual thread feature in Java 19.

Using Virtual Threads in Java

First, we need to install JDK 19 from Oracle or OpenJDK.

After installing JDK19 successfully, we create the App.java file as follows to test the virtual thread creation feature.

Because the virtual thread feature is still in preview, it is necessary to run the java command with the –enable-preview option as follows:

java --source 19 --enable-preview ./App.java

Configure the test machine using windows 11 with i7 8750H CPU and 16GB of RAM. Run results are as follows:

As we can see, in Java the use of virtual threads is more than 30 times more efficient than regular threads.

A few things to keep in mind

The creation of a virtual thread is very fast and efficient compared to a regular thread, but the computational performance on the virtual thread is only equivalent to that of a regular thread. Therefore, using virtual thread is only beneficial when we need to create a large number of threads to do low CPU performance tasks, such as network processing, file reading/writing, database queries. Using virtual threads also makes it possible for the system to exceed the operating system’s maximum thread count, which is limited by hardware configuration.

Currently, the virtual thread on Java is still only a preview, so it needs to be activated with the –enable-preview option.

Conclude

Virtual thread in Java is a very useful feature, which promises to bring a lot of changes in the design and construction of Java-based systems, especially systems that need a lot of parallel processing. Currently, some large projects in the Java ecosystem have started using virtual threads such as Quarkus, Tomcat, Helidon, …

Reference links

Java Virtual Threads

Intro to virtual threads

How to use Java 19: Virtual Threads

Virtual thread performance gain for Microservices

Share the news now

Source : Viblo