Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:


1. Overview

In the ever-evolving landscape of Java development, the introduction of Java 21 brought forth a revolutionary feature – virtual threads. These lightweight threads, managed by the Java Virtual Machine (JVM), promise to reshape how developers approach concurrency in Java applications. Concurrent application development has long been challenging, often fraught with complexities when managing traditional OS-managed threads.

At its core, the Quarkus framework is a modern, developer-centric toolkit designed for the cloud-native era. It boasts lightning-fast startup times and low memory consumption while offering developers an extensive set of tools for building microservices and cloud-native applications.

In this tutorial, we’ll discover how Quarkus leverages Java’s virtual threads, transforming how concurrency is managed in Java applications.

2. Understanding Concurrency in Java

Java’s journey in managing threads has undergone a significant transformation since its inception. Initially, Java utilized green threads – user-level threads managed by the JVM – emulating multithreading without relying on the native operating system’s capabilities. However, this approach was short-lived and evolved into integrating OS-managed threads in later versions of Java.

Traditional threading models in Java, relying on OS-managed threads, posed several challenges. The imperative and reactive models governed the development landscape, each with its strengths and limitations. The imperative model, straightforward in its approach, faced limitations in scalability due to the constraints of OS threads. In contrast, the reactive model, although efficient, demanded a paradigm shift in coding patterns, making it complex and sometimes non-intuitive for developers.

3. Introducing Virtual Threads

Java 21’s introduction of virtual threads marks a paradigm shift in concurrency handling. Virtual threads, managed by the JVM, offer a compelling alternative to traditional OS-managed threads. These threads are lightweight entities that promise enhanced concurrency while consuming significantly fewer resources compared to their OS counterparts.

Virtual threads bring forth a multitude of advantages, including improved scalability and resource utilization. Unlike OS threads, which are resource-intensive, virtual threads are lightweight and can be created in larger numbers without significantly impacting system resources. This efficiency in resource utilization opens doors for better concurrency handling in Java applications.

4. Contextualizing Virtual Threads in Quarkus

Understanding how virtual threads integrate within the Quarkus framework provides insights into their practical implementation. Quarkus, designed for cloud-native applications, emphasizes efficiency and performance without compromising developer productivity.

Quarkus leverages virtual threads to enhance its concurrency model, allowing developers to write imperative-style code while benefiting from the advantages of virtual threads. By seamlessly integrating virtual threads into its architecture, Quarkus provides a modern and efficient platform for developing highly concurrent applications.

5. Implementation in Quarkus

To implement virtual threads in Quarkus, we can make the following adjustments to our project.

5.1. Dependency Configuration

We need to include the necessary dependency in our pom.xml file:


Additionally, we must ensure that our project is configured to use Java 21 or a higher version:


5.2. Leveraging Virtual Threads Annotations

When integrating virtual threads into our Quarkus application, the key mechanism is the utilization of specific annotations, most notably @RunOnVirtualThread. This annotation serves as a guiding directive, instructing the system to execute designated methods or operations on virtual threads as opposed to the conventional platform threads.

For example, to facilitate interaction with a remote service, the creation of a remote service interface is imperative. The interface defines the necessary communication protocols:

public class VirtualThreadApp {
    RemoteService service;
    public String process() {
        var response = service.greetings();
        return response.toUpperCase();

Within this class, the selective application of @RunOnVirtualThread to the process() method serves as a specific directive. This annotation ensures that this method is executed on virtual threads, allowing for streamlined and efficient handling of operations, such as invoking a remote service. This targeted application of virtual threads enhances the overall concurrency management within the class.

6. Performance Comparisons: Traditional vs. Virtual Threads

An in-depth exploration of the performance disparities between traditional threading models and virtual threads within Quarkus applications provides crucial insights into their operational efficiencies. Through benchmarking tests evaluating scalability, resource utilization, and responsiveness across diverse workloads, we can uncover the distinct advantages that virtual threads offer over their traditional counterparts.

The comparative analysis showcases the superior performance of virtual threads, highlighting their efficiency in managing concurrency. Benchmark results underscore the benefits of virtual threads in terms of enhanced scalability, optimized resource utilization, and improved responsiveness under varying application loads. This empirical evaluation serves as a valuable reference for developers aiming to make informed decisions about the concurrency model best suited for their Quarkus applications.

7. Challenges and Considerations

In the dynamic landscape of virtual thread utilization, several challenges and considerations merit attention. These aspects play a pivotal role in ensuring a seamless and optimized experience with virtual threads in Quarkus applications.

7.1. Pinning Issues

Instances may arise where virtual threads encounter blocking due to holding locks or native calls. Overcoming this challenge involves identifying such scenarios and reworking code segments to prevent carrier thread blocking.

7.2. Monopolization Concerns

Long-running computations executed by virtual threads can monopolize carrier threads, potentially impacting the application’s responsiveness. Strategies to manage and optimize thread utilization for intensive computations are essential.

7.3. Memory Usage and Thread Pool Optimization

Optimizing thread pools and managing memory usage becomes critical when leveraging virtual threads. Careful consideration of thread pool configurations and memory management prevents excessive thread pool elasticity and memory overhead.

7.4. Ensuring Thread Safety

Maintaining thread-safe implementations in a virtual thread environment is crucial to prevent data inconsistencies or race conditions when multiple virtual threads access shared resources concurrently.

8. Best Practices and Recommendations

Using virtual threads effectively requires following best practices and recommendations to ensure optimal performance and maintainability.

8.1. Strategies for Optimizing Virtual Thread Usage

To optimize virtual thread usage, we need to:

  • Identify Blocking Operations: Analyze and minimize code segments that cause virtual threads to block, ensuring smoother execution.
  • Use Asynchronous Operations: Implement non-blocking I/O and asynchronous processing to increase virtual thread concurrency and efficiency.
  • Monitor Thread Pools: Regularly check and adjust thread pool configurations to optimize resource use and prevent unnecessary expansion.

8.2. Recommendations for Developers

The following can be considered as recommendations:

  • Focus on Thread Safety: Ensure thread safety in shared resources to avoid data inconsistencies and race conditions.
  • Continuously Refactor: Regularly update and improve code for efficient, non-blocking execution.
  • Share Knowledge: Engage in collaborative learning by sharing experiences and best practices about virtual threads to collectively overcome challenges and enhance efficiency.

9. Conclusion

In this article, we delved into the adoption of virtual threads in Quarkus, shedding light on its plethora of benefits, including enhanced concurrency, optimized resource utilization, and improved scalability. However, we saw that challenges like thread pinning, monopolization, and memory management demand meticulous consideration and strategic handling to fully reap the benefits of virtual threads.

The complete source code for this tutorial is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Notify of
Inline Feedbacks
View all comments