A Look at Virtual Threads in a Jakarta EE Managed Context

Jakarta EE

Upcoming Java 21 brings a bunch of new features, including very interesting progress in concurrency. There are two main improvements – virtual threads and structured concurrency.

In this article, we discuss incoming usage of these enhancements inJakarta EE (formerly Java EE, now an open source project maintained by the Eclipse Foundation), a set of specifications that enables the worldwide community of Java developers to work on cloud native Java enterprise applications. Jakarta Concurrency is the concrete specification describing the expected behaviour.

The support in Jakarta EE is currently under development, details may change in the final implementation.

Jakarta Concurrency

A quick reminder, this is where Jakarta Concurrency is used in the server. See the orange-colored part of the diagram!

Source: https://jakarta.ee/specifications/concurrency/3.0/jakarta-concurrency-spec-3.0.html#container-thread-context

What are the Virtual Threads?

Virtual threads improve application throughput, which is the biggest value for server-side Java applications. Java Virtual Machine (JVM) handles the virtual threads in a more effective way, as there are less real (platform, carrier, operating system) threads running the code. Java maps the required work in virtual threads to platform threads. Whenever the thread is blocked (e.g. waiting for database, REST call etc.), the platform thread switches to execute another virtual thread. The relation between virtual and platform threads is shown here:

Source: https://forums.oracle.com/ords/apexds/post/java-virtual-threads-easy-introduction-4120

There are several more advantages. Less memory is required for multiple threads, it is kept in Java heap instead of limited native memory. They are kept in Java, so they don’t reach the Thread’s OutOfMemory if too many of them are used.

Virtual Threads in Jakarta EE 11

Jakarta 11is the upcoming new release of the Jakarta EE Platform, with a target release date of the first quarter of 2024, roughly six months after the target release for Java 21.

In Jakarta EE, applications run inside servers. The servers provide resources, in this case threads. They enhance threads by context (access to database, HTTP requests, logged in user etc.) and also allow administrators control the behavior of the server by setting up thread pools, setting for example priority or pool size…

This means, that programmers don’t create threads manually, but rely on Jakarta EE API. The most common way is to use @ManagedExecutorService:

1.      @Resource(lookup = "java:app/concurrent/myExecutor")
2.      private ManagedExecutorService managedExecutor;
3.
4.     public String longJob() {
5. managedExecutor.submit(() -> {
6. backService.longJob();
7. });
8. return "Job Submitted";
9.     }

This is the typical way, the server provides platform threads and the application runs in parallel. Now, how to use the virtual threads instead:

1.  @ManagedExecutorDefinition(name = "java:app/concurrent/myExecutor", 
2.   maxAsync = 3, virtual = true)

This simple change will improve the throughput of existing applications!

Similarly, @ManagedThreadFactory (MTF) and the scheduled version of MES and MTF will be updated. Details can be found in the enhancement ticket: https://github.com/jakartaee/concurrency/issues/268

Structured Concurrency

The second big improvement of Java 21 is structured concurrency. It is replacing thread locals with a safer way. Instead of setting up thread local, the user specifies the shared objects in try block. At the end of the block, the object is no longer shared, e.g. it cannot be forgotten in thread local. For example:

1.  try (var scope = new StructuredTaskScope<Object>("MyTaskScopeWithContext",
managedThreadFactory
) {
2. Future<Long> future1 = scope.fork(task1);
3. Future<Long> future2 = scope.fork(task2);
4.       scope.join();
5. ...
6. }

This code will work with the current Jakarta EE implementations as the managed factories set up the thread’s context.

Enhancements in Jakarta EE are discussed in ticket https://github.com/jakartaee/concurrency/issues/272

Further Reading

 

Payara Enterprise

Build Fast and Secure. ✅Supported.✅ Best for Jakarta EE and MicroProfile.✅

TRY FREE.

Comments (0)

Post a comment

Your email address will not be published. Required fields are marked *

Payara needs the contact information you provide to us to contact you about our products and services. You may unsubscribe from these communications at any time. For information on how to unsubscribe, as well as our privacy practices and commitment to protecting your privacy, please review our Legal & Privacy Policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Docker logo 4 minutes
Community

Level Up Your Jakarta EE Apps with Payara 7 and New Docker Images

We’re excited to share major updates around the Docker image story for the Payara Platform Community, aligned with our […]

Patrik Dudits presenting at Devoxx Belgium 2025 5 minutes
Cloud & Microservices

Devoxx BE 2025: It Only Starts with a Container & How Abstraction Becomes Reality 

At Devoxx Belgium 2025, I was able to talk about what happens after you build your container. In theory, […]

Blue background with coral and fish. Left text: 'MONTHLY CATCH'. Right: laptop screen with tech tabs and Payara Community logo. 5 minutes
Community

The Payara Monthly Catch – October 2025

Welcome aboard the October issue of The Monthly Catch!As the leaves turn and conference season hits full stride, the […]