Jakarta Concurrency and Managed Executors Explained
Learn Jakarta Concurrency: ManagedExecutorService, scheduled executors, context propagation, the Asynchronous annotation and virtual threads.

Inside a Jakarta EE application server, creating your own threads is a mistake that works in a demo and fails in production. The server manages the threads, the security identity, the transaction and the class loader of each request, and a raw new Thread() or Executors.newFixedThreadPool knows nothing about any of them. Jakarta Concurrency is the specification that gives you executors the container controls. This post explains why raw pools fail, shows the managed executor, the scheduled executor and the @Asynchronous annotation, covers context propagation, defines a custom executor and describes what changed in version 3.1 with virtual threads. It targets Jakarta EE 11 on JDK 21 or later.
Why not Executors
A thread created by the application is invisible to the server. It does not appear in the server's monitoring, it is not stopped when the application is undeployed, and it holds a reference to the application's class loader, so a redeploy leaks the entire old application in memory. It runs without the caller's security principal, so isCallerInRole and @RolesAllowed fail or answer for nobody. It has no JNDI context, so a lookup in the task fails. It has no transaction, and a call that expects one throws. And the pool's size is the application's decision, not the operator's, so two applications on one server each start fifty threads and nobody sets the total.
Jakarta Concurrency fixes these by making the container hand out the executors. The servers (WildFly, Payara, Open Liberty) configure thread counts, queue sizes and context behaviour for them in one place.
A managed executor
@ApplicationScoped
public class ReportService {
@Resource
ManagedExecutorService executor;
public CompletableFuture<Report> build(long customerId) {
return executor.supplyAsync(() -> generate(customerId));
}
private Report generate(long customerId) { /* slow work */ return new Report(customerId); }
}ManagedExecutorService extends ExecutorService, so submit and invokeAll work, and it also offers supplyAsync and runAsync that return a CompletableFuture which runs its stages on the managed pool. @Resource injects the default instance; @Inject also works in current versions. Use the future as in any asynchronous pipeline, as covered in CompletableFuture pipelines. The executor is a shared pool, so keep tasks short or bounded and never block indefinitely inside them, because enough stuck tasks starve the rest of the application, a failure that looks like a hang and is diagnosed with a thread dump, see deadlocks in thread dumps.
Scheduled and timed work
For delayed or periodic tasks, inject ManagedScheduledExecutorService:
@Resource
ManagedScheduledExecutorService scheduler;
@PostConstruct
void start() {
scheduler.scheduleWithFixedDelay(this::purgeExpired, 1, 10, TimeUnit.MINUTES);
}It behaves like ScheduledExecutorService with container context. On a cluster, every node runs the schedule, so a job that must run once needs coordination, such as a database lock. Jakarta Concurrency 3.0 adds triggers for cron-like schedules through Schedule and the @Asynchronous annotation's runAt attribute, which run a method on a cron expression. For persistent timers that survive a restart, use the EJB timer service or a scheduler library; managed executors do not persist their schedules.
Jakarta EE also includes ManagedThreadFactory, for libraries that need to create their own threads: pass it to the library so its threads are container-managed.
The annotation
Since Jakarta Concurrency 3.0 you can make a method asynchronous with an annotation on a CDI bean:
@ApplicationScoped
public class Notifications {
@Asynchronous
public CompletionStage<Void> sendWelcome(String email) {
mailer.send(email, "Welcome");
return Asynchronous.Result.complete(null);
}
}The container runs the call on a managed executor and returns immediately; the method returns a CompletionStage, and the caller gets that stage right away. The method must return void, CompletionStage or CompletableFuture, and the return value is built with Asynchronous.Result. Exceptions complete the stage exceptionally. The annotation relies on CDI interception, so it has the same limitation as other interceptor-based features: a call from another method of the same class does not go through the proxy and runs synchronously. This mirrors the Spring proxy rule in self-invocation.
Context propagation
The point of managed executors is that context travels with the task. By default the container captures, at submission time, the context of the submitting thread and applies it while the task runs: the class loader, the JNDI naming context, and the security context. Transaction context is handled differently: a task does not join the caller's transaction, since it runs in another thread at another time. If the task needs a transaction, it starts its own, for example with @Transactional on a method of a CDI bean it calls.
That separation is important for correctness. A task that is submitted inside a transaction and expects its changes to be visible may start before the caller commits, and reads old data. Submit after the commit, or have the task read what it needs from parameters, not from the database.
Defining an executor
Rather than use the server's default executor for everything, define separate ones with bounded sizes, so a slow report cannot starve payment processing:
@ManagedExecutorDefinition(name = "java:app/concurrent/ReportExecutor",
maxAsync = 4,
hungTaskThreshold = 120_000)
@ApplicationScoped
public class ExecutorConfig { }Then inject it by name with @Resource(lookup = "java:app/concurrent/ReportExecutor"). maxAsync caps the number of concurrently running tasks, and hungTaskThreshold tells the server to flag tasks running longer than that many milliseconds. Definitions can also come from web.xml or the server's console, where an operator can adjust them without a rebuild; the sizes depend on the workload and the server's capacity, so measure rather than copy numbers. Pool sizing principles apply here as they do elsewhere, see thread pool size for CPU and I/O work.
Virtual threads in 3.1
Jakarta Concurrency 3.1, part of Jakarta EE 11, integrates with virtual threads. The attribute virtual = true on a @ManagedExecutorDefinition (and the equivalent for scheduled executors and thread factories) makes the container run each task on a virtual thread, when the server runs on JDK 21 or later. The concurrency limit is then not the pool size but the executor's maxAsync, if you set one, so keep a cap where a downstream resource is scarce. Context propagation still applies. Support depends on the server version, so check the release notes of your server before relying on it. The general model for virtual threads is in virtual threads explained.
Where this does not apply
Outside an application server, for example in Spring Boot or Quarkus, thread management is done by the framework's own executors and @Async, and Jakarta Concurrency, where present in Quarkus, is limited. For plain Java SE, use java.util.concurrent directly. The tutorials are at the Jakarta EE tutorial, and the specification is at jakarta.ee/specifications/concurrency.


