Virtual Threads in Java 21

EL MAALMI
3 min readSep 16, 2023

--

Java continues to be a foundational language for enterprise-level applications despite the constantly changing landscape of computer languages. Java constantly adds new features that improve developer productivity, security, and performance. The advent of Virtual Threads, which debuted in Java 20 and was improved upon in Java 21, is one such innovative feature.

What is Threads in Java ?

Java’s threads have long been a key element, enabling the concurrent execution of tasks inside of a program. For tasks like managing several network connections, processing data, or doing complicated computations, they offer a way to carry out multiple processes simultaneously.

Traditional threads are resource-intensive by nature and consume a large amount of system resources to maintain. Each thread has its own stack and frequently switches between them, which can result in inefficiencies, especially when there are many threads involved.

The Birth of Virtual Threads

A new breed of threads called virtual threads, commonly referred to as lightweight threads or fibers, was developed to solve the drawbacks of conventional threads. They are made to be lighter and more effective, enabling programmers to construct several threads with little extra overhead.

Virtual threads lack a native stack, in contrast to conventional threads. Instead, they share the underlying thread’s stack. As a result, memory usage is greatly decreased, and a large number of virtual threads can be created without taxing the system’s resources.

Advantages of Virtual Threads

1. Improved Scalability :

High scalability is a key component of Virtual Threads’ design. It is now possible to create thousands of threads without using up all of the system resources thanks to the decreased overhead.

2. Simplified Concurrent Programming :

Virtual Threads simplify the process of writing concurrent code. Developers can write code as if it were single-threaded, which makes it easier to reason about and less error-prone.

3. Simplified Synchronization :

Virtual threads can often rely on simpler synchronization mechanisms compared to traditional threads. This can lead to cleaner, more maintainable code.

Getting Started with Virtual Threads

public class Main{
public static void main(String[] args) {
// Creating a virtual thread
Thread.ofVirtual().start(() -> {
for (int i = 1; i <= 50; i++) {
System.out.println("Virtual Thread: " + i);
try {
Thread.sleep(1000); // Simulate some work
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});

// Continue with the main thread
for (int i = 1; i <= 5; i++) {
System.out.println("Main Thread: " + i);
try {
Thread.sleep(1000); // Simulate some work
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

In this example, we’re creating a virtual thread using Thread.ofVirtual().start(). Inside the virtual thread, we have a loop that prints out messages and simulates some work with a Thread.sleep().

Meanwhile, the main thread continues executing its own loop and printing its own messages.

When we run this program, we will see output from both the virtual thread and the main thread interleaved, demonstrating concurrent execution.

Conclusion :

Finally, Virtual Threads represent a big improvement in the Java platform, resolving persistent thread management problems. They open up new opportunities for creating highly concurrent and responsive applications by offering a more portable and scalable threading mechanism.

Resources :

--

--

EL MAALMI

Passionate software engineering graduate skilled in Java Spring, JavaScript, clean code, React, and Docker and a junior pentest .