A Look at Virtual Threads in a Jakarta EE Managed Context

Photo of Petr Aubrecht by Petr Aubrecht

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.

Related Posts

Comments