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:
Threads | Virtual Threads |
---|---|
Created and managed by the operating system | Created and managed by platform |
Consumes a lot of resources | Consume less resources |
Difficult to manage | Easy to manage |
A few | Greater 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | public class App { public static void main( String[] args ) { Random random = new Random(); Runnable runnable = () -> { double i = random.nextDouble(1000) % random.nextDouble(1000); }; int numThread = 100000; long start = System.currentTimeMillis(); for (int i = 0; i < numThread; i++) { Thread.startVirtualThread(runnable); } long finish = System.currentTimeMillis(); long timeElapsed = finish - start; System.out.println("Run time with virtual thread: " + timeElapsed); start = System.currentTimeMillis(); for (int i = 0; i < numThread; i++) { Thread t = new Thread(runnable); t.start(); } finish = System.currentTimeMillis(); timeElapsed = finish - start; System.out.println("Run time without virtual thread: " + timeElapsed); } } |
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:
1 2 3 | Run time with virtual thread: 488 Run time without virtual thread: 16652 |
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, …