javax.util.concurrent
Interface ManagedThreadFactory

All Superinterfaces:
java.util.concurrent.ThreadFactory

public interface ManagedThreadFactory
extends java.util.concurrent.ThreadFactory

A manageable version of a ThreadFactory.

A ManagedThreadFactory provides a method for creating threads for execution in a managed environment. Implementations of the ManagedThreadFactory are provided by a Java™ 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 Concurrency Utilities for Java™ EE 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.

The Runnable task that is allocated to the new thread using the ThreadFactory.newThread(java.lang.Runnable) method will run with the application component context of the component instance that created (looked-up) this ManagedThreadFactory instance.

The task runs without an explicit transaction (they do not enlist in the application component's transaction). If a transaction is required, use a javax.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:      javax.util.concurrent.ManagedThreadFactory
  *  jndi-name: concurrent/tf/DefaultThreadFactory
  */
 public ExecutorService getManagedThreadPool() {
     InitialContext ctx = new InitialContext();
     ManagedThreadFactory tf = (ManagedThreadFactory)
         ctx.lookup("java:comp/env/concurrent/tf/DefaultThreadFactory");

     // All threads will run as part of this application component.
     return new ThreadPoolExecutor(5, 10, 5, TimeUnit.SECONDS,
         new ArrayBlockingQueue<Runnable>(10), tf);
 }
 

Author:
Chris D Johnson

Method Summary
 
Methods inherited from interface java.util.concurrent.ThreadFactory
newThread