Day 16 — Enterprise Concurrency & Managed Threads¶
Daily Time Investment: 2.0 hours | Week: 3 | Phase: 3
Daily Schedule¶
| Segment | Duration | Activity |
|---|---|---|
| Core Theory | 45 min | Why raw threads are forbidden, ManagedExecutorService, ManagedThreadFactory, context propagation, CompletableFuture parallel orchestration |
| Book Reading | 30 min | EE Patterns Ch3 (Async Facade section) + EE8 AppDev Ch13 (Servlet Async Processing) |
| Hands-On Lab | 45 min | Context-propagating managed executor; verify parallel fan-out is ~175ms vs ~340ms sequential |
Files in This Day¶
-
EE8 AppDev Ch13 — Servlet Async Processing
@WebServlet(asyncSupported=true),AsyncContext,request.startAsync(),asyncContext.complete(), async vs managed threads comparison. HTTP/2 Server Push. Servlet lifecycle scopes (request/session/application). -
Lab Guide — Enterprise Concurrency & Managed Threads
ManagedExecutorServicewith context propagation (Security Principal + Correlation ID),CompletableFuture.allOf()parallel fan-out for 4 nodes (~175ms vs ~340ms sequential), non-blocking timeout fallback,.exceptionally()error recovery, clean lifecycle shutdown.
Prerequisites to Continue¶
New concepts not seen in Phase 1 or Phase 2
- Why
new Thread()is BANNED in enterprise containers — raw threads bypass the container's classloader boundaries, lack security/transaction/CDI context, cannot be gracefully shut down during undeployment, and can crash the JVM with unbounded creation. The Jakarta EE specification explicitly forbids them ManagedExecutorService— Jakarta Concurrency 3.0's container-managed thread pool; obtained via@Resource ManagedExecutorService executor; captures caller context (security principal, correlation ID) at submission time and restores it on the worker threadManagedThreadFactory— creates container-aware threads with structured naming; the factory enforces container lifecycle governance- Context Propagation — when you submit a
RunnableorSuppliertoManagedExecutorService, the container snapshots the calling thread'sSecurityContext, CDI context, and transaction state, then restores them on the worker thread before the task runs CompletableFuture.supplyAsync(supplier, managedExecutor)— bridges Java's async API with the enterprise container's managed thread pool; NEVER useCompletableFuture.supplyAsync(supplier)without the executor argument in enterprise code — it uses the JVM's commonForkJoinPool, which has no container context@Asynchronous— EJB annotation that makes a business method execute asynchronously; returnsFuture<T>orAsyncResult<T>; simpler than JMS for lightweight async, but tied to EJB