DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Java and Low Latency
  • Sustainable Java Applications With Quick Warmup
  • Five Java Developer Must-Haves for Ultra-Fast Startup Solutions
  • Ways To Reduce JVM Docker Image Size

Trending

  • Doris: Unifying SQL Dialects for a Seamless Data Query Ecosystem
  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  • Issue and Present Verifiable Credentials With Spring Boot and Android
  • AI-Based Threat Detection in Cloud Security
  1. DZone
  2. Coding
  3. Java
  4. The Future of Java: Virtual Threads in JDK 21 and Their Impact

The Future of Java: Virtual Threads in JDK 21 and Their Impact

The article discusses the addition of virtual threads to JDK 21 and their impact on the future of Java.

By 
Roopa Kushtagi user avatar
Roopa Kushtagi
·
Oct. 23, 23 · Review
Likes (11)
Comment
Save
Tweet
Share
9.7K Views

Join the DZone community and get the full member experience.

Join For Free

JDK 21, released on September 19, 2023, marks a significant milestone in Java's evolution. It's a long-term support (LTS) release, ensuring stability and support from Oracle for at least eight years. In this release, Java developers are introduced to several new features, including virtual threads, record patterns, pattern matching for switch statements, the foreign function and memory API, and the ZGC garbage collector.

The Significance of Virtual Threads

Among these features, virtual threads stand out as a game-changer in the world of concurrent Java applications. Virtual threads have the potential to revolutionize the way developers write and manage concurrent code. They offer exceptional benefits, making them ideal for high-throughput concurrent applications.

Advantages of Virtual Threads

Virtual threads bring several advantages to the table, differentiating them from traditional platform threads:

  • Efficient Memory Usage: Virtual threads consume significantly less memory compared to their traditional counterparts. The JVM intelligently manages memory for virtual threads, enabling reuse and efficient allocation for multiple virtual threads.
  • Optimized CPU Usage: Virtual threads are more CPU-efficient. The JVM can schedule multiple virtual threads on the same underlying OS thread, eliminating the overhead of context switching.
  • Ease of Use: Virtual threads are easier to work with and manage. The JVM takes care of their lifecycle, removing the need for manual creation and destruction. This simplifies concurrent Java application development and maintenance.

Understanding Types of Threads

To grasp the significance of virtual threads, let's briefly examine three types of threads:

1. OS Threads (Native Threads): These are the fundamental units of execution in modern operating systems. Each OS thread has its own stack, program counter, and register set, managed by the OS kernel.

2. Platform Threads (Traditional Java Threads): Platform threads are built on top of OS threads but managed by the Java Virtual Machine (JVM). They offer more efficient scheduling and resource utilization. A platform thread is an instance of java.lang.Thread implemented in the traditional way, as a thin wrapper around an OS thread. 

3. Virtual Threads: Virtual threads are instances of java.lang.Thread is not bound to specific OS threads. Multiple virtual threads can be served by a single OS thread. Virtual threads are even lighter weight and more efficient than traditional threads, and they can be used to improve the performance, scalability, and reliability of concurrent Java applications.

The Challenge of Traditional Threads

Threads are the building blocks of concurrent server applications and have been integral to Java for nearly three decades. Unfortunately, traditional Java threads are expensive to create and maintain. The number of available threads is limited because the JDK implements threads as wrappers around operating system (OS) threads. OS threads are costly, so we cannot have too many of them, which makes the implementation ill-suited to the thread-per-request style limiting the number of concurrent requests that a Java server can handle and thus impacting the server application scalability.

Thread-per-request style

The Shift to Thread-Sharing

Over a period of time, to be able to scale and to utilize hardware to its fullest, java developers transitioned from the thread-per-request style to a thread-sharing style. In thread-sharing, instead of handling a request on one thread from start to finish, the request-handling code returns its thread to a pool when it waits for another I/O operation to complete so that the thread can service other requests.

This fine-grained sharing saves threads but requires asynchronous programming techniques. Asynchronous programming is a complex and error-prone way to improve scalability, and it is not compatible with the thread-per-request style that is common in Java server applications.

Introducing Virtual Threads

Virtual threads offer a compelling solution to preserve the thread-per-request style while enhancing concurrency. They are not tied to specific OS threads, allowing for greater concurrency. Virtual threads consume OS threads only during CPU-bound tasks. When they encounter blocking I/O operations, they are automatically suspended without monopolizing an OS thread. Creating and managing virtual threads is straightforward, eliminating the need for pooling.

Virtual threads are short-lived, perform specific tasks, and are plentiful. Platform threads are heavyweight, long-lived, and may need pooling. Using virtual threads maintains the thread-per-request style while optimizing hardware utilization. 

The Future With JDK 21

It's essential to note that with JDK 21, the goal is not to replace traditional thread implementations but to provide an efficient alternative. Developers retain the flexibility to choose between virtual threads and platform threads based on their specific requirements.

Here's a simple example that showcases the power of virtual threads:

 
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {

     IntStream.range(0, 10_000).forEach(i -> {

         executor.submit(() -> {

             Thread.sleep(Duration.ofSeconds(1));

             return i;

         });

     });

     // Wait for all tasks to complete.

} // The executor is automatically closed, and it waits for task completion.


In this example, modern hardware efficiently handles 10,000 virtual threads concurrently, executing straightforward code. Behind the scenes, the JDK efficiently manages these virtual threads, optimizing resource utilization.

Conclusion

Java developers have long relied on threads for concurrent programming. With the introduction of virtual threads in JDK 21, achieving high concurrency while preserving a familiar programming style becomes easier. Virtual threads are especially valuable for server applications that handle numerous user requests, offering efficiency and scalability in a changing landscape.

Java Development Kit Java virtual machine applications Java (programming language) operating system Virtual Machine

Published at DZone with permission of Roopa Kushtagi. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Java and Low Latency
  • Sustainable Java Applications With Quick Warmup
  • Five Java Developer Must-Haves for Ultra-Fast Startup Solutions
  • Ways To Reduce JVM Docker Image Size

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!