-
- All Superinterfaces:
ForkJoinPool.ForkJoinWorkerThreadFactory
,ThreadFactory
public interface ManagedThreadFactory extends ThreadFactory, ForkJoinPool.ForkJoinWorkerThreadFactory
A manageable version of aThreadFactory
.A ManagedThreadFactory extends the Java™ SE ThreadFactory to provide a method for creating threads for execution in a Jakarta™ EE environment. Implementations of the ManagedThreadFactory are provided by a Jakarta EE Product Provider. Application Component Providers use the Java Naming and Directory Interface™ (JNDI) to look-up instances of one or more ManagedThreadFactory objects using resource environment references.
The Jakarta Concurrency specification describes several behaviors that a ManagedThreadFactory can implement. The Application Component Provider and Deployer identify these requirements and map the resource environment reference appropriately.
Threads returned from the
newThread()
method should implement theManageableThread
interface. The Runnable task that is allocated to the new thread using theThreadFactory.newThread(Runnable)
method will run with the application component context of the component instance that created (looked-up) this ManagedThreadFactory instance.The
ForkJoinWorkerThread
that is created by theForkJoinPool.ForkJoinWorkerThreadFactory.newThread(ForkJoinPool)
method runs tasks with the application component context of the component instance that created (looked-up) this ManagedThreadFactory instance. The Jakarta EE Product Provider establishes the context once perForkJoinWorkerThread
and does not reset the context between operations that run on theForkJoinWorkerThread
.The task runs without an explicit transaction (they do not enlist in the application component's transaction). If a transaction is required, use a
jakarta.transaction.UserTransaction
instance. A UserTransaction instance is available in JNDI using the name: "java:comp/UserTransaction"Example:
public run() { // Begin of task InitialContext ctx = new InitialContext(); UserTransaction ut = (UserTransaction) ctx.lookup("java:comp/UserTransaction"); ut.begin(); // Perform transactional business logic ut.commit(); }
A ManagedThreadFactory can be used with Java SE ExecutorService implementations directly.Example:
/** * Create a ThreadPoolExecutor using a ManagedThreadFactory. * Resource Mappings: * type: jakarta.enterprise.concurrent.ManagedThreadFactory * jndi-name: concurrent/tf/DefaultThreadFactory */ @Resource(name="concurrent/tf/DefaultThreadFactory") ManagedThreadFactory tf; public ExecutorService getManagedThreadPool() { // All threads will run as part of this application component. return new ThreadPoolExecutor(5, 10, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10), tf); }
ForkJoinPool Example:
ManagedThreadFactory threadFactory = InitialContext.doLookup("java:comp/DefaultManagedThreadFactory"); ForkJoinPool pool = new ForkJoinPool( Runtime.getRuntime().availableProcessors(), threadFactory, null, false); ForkJoinTask<Double> totals = pool.submit(() -> orders .parallelStream() .map(order -> { if (order.total == 0.0) { // lookups require application component namespace: try (Connection con = ((DataSource) InitialContext.doLookup( "java:comp/env/jdbc/ds1")) .getConnection()) { order.total = ... } catch (NamingException | SQLException x) { throw new CompletionException(x); } } return order.total; }) .reduce(0.0, Double::sum)); System.out.println("Sum is: " + totals.join()); pool.shutdown();
- Since:
- 1.0
-
-
Method Summary
-
Methods inherited from interface java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory
newThread
-
Methods inherited from interface java.util.concurrent.ThreadFactory
newThread
-
-