java.util.concurrent
Class Executors

java.lang.Object
  extended by java.util.concurrent.Executors

public class Executors
extends Object

Factory and utility methods for Executor, ExecutorService, ThreadFactory, Future, and Cancellable classes defined in this package.

Since:
1.5

Method Summary
static ThreadFactory defaultThreadFactory()
          Return a default thread factory used to create new threads.
static
<T> Future<T>
execute(Executor executor, Callable<T> task)
          Executes a value-returning task and returns a Future representing the pending results of the task.
static Future<Object> execute(Executor executor, PrivilegedAction action)
          Executes a privileged action under the current access control context and returns a Future representing the pending result object of that action.
static Future<Object> execute(Executor executor, PrivilegedExceptionAction action)
          Executes a privileged exception action under the current access control context and returns a Future representing the pending result object of that action.
static Cancellable execute(Executor executor, Runnable task)
          Executes a Runnable task and returns a Cancellable representing that task.
static
<T> Future<T>
execute(Executor executor, Runnable task, T value)
          Executes a Runnable task and returns a Future representing that task.
static
<T> T
invoke(Executor executor, Callable<T> task)
          Executes a value-returning task and blocks until it returns a value or throws an exception.
static void invoke(Executor executor, Runnable task)
          Executes a Runnable task and blocks until it completes normally or throws an exception.
static ExecutorService newCachedThreadPool()
          Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.
static ExecutorService newCachedThreadPool(ThreadFactory threadFactory)
          Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available, and uses the provided ThreadFactory to create new threads when needed.
static ExecutorService newFixedThreadPool(int nThreads)
          Creates a thread pool that reuses a fixed set of threads operating off a shared unbounded queue.
static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory)
          Creates a thread pool that reuses a fixed set of threads operating off a shared unbounded queue, using the provided ThreadFactory to create new threads when needed.
static ExecutorService newSingleThreadExecutor()
          Creates an Executor that uses a single worker thread operating off an unbounded queue.
static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory)
          Creates an Executor that uses a single worker thread operating off an unbounded queue, and uses the provided ThreadFactory to create new threads when needed.
static ThreadFactory privilegedThreadFactory()
          Return a thread factory used to create new threads that have the same permissions as the current thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

newFixedThreadPool

public static ExecutorService newFixedThreadPool(int nThreads)
Creates a thread pool that reuses a fixed set of threads operating off a shared unbounded queue. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.

Parameters:
nThreads - the number of threads in the pool
Returns:
the newly created thread pool

newFixedThreadPool

public static ExecutorService newFixedThreadPool(int nThreads,
                                                 ThreadFactory threadFactory)
Creates a thread pool that reuses a fixed set of threads operating off a shared unbounded queue, using the provided ThreadFactory to create new threads when needed.

Parameters:
nThreads - the number of threads in the pool
threadFactory - the factory to use when creating new threads
Returns:
the newly created thread pool

newSingleThreadExecutor

public static ExecutorService newSingleThreadExecutor()
Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. This method is equivalent in effect to new FixedThreadPool(1).

Returns:
the newly-created single-threaded Executor

newSingleThreadExecutor

public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory)
Creates an Executor that uses a single worker thread operating off an unbounded queue, and uses the provided ThreadFactory to create new threads when needed.

Parameters:
threadFactory - the factory to use when creating new threads
Returns:
the newly-created single-threaded Executor

newCachedThreadPool

public static ExecutorService newCachedThreadPool()
Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors.

Returns:
the newly created thread pool

newCachedThreadPool

public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory)
Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available, and uses the provided ThreadFactory to create new threads when needed.

Parameters:
threadFactory - the factory to use when creating new threads
Returns:
the newly created thread pool

execute

public static Cancellable execute(Executor executor,
                                  Runnable task)
Executes a Runnable task and returns a Cancellable representing that task.

Parameters:
executor - the Executor to which the task will be submitted
task - the task to submit
Returns:
a Cancellable representing pending completion of the task
Throws:
RejectedExecutionException - if task cannot be scheduled for execution

execute

public static <T> Future<T> execute(Executor executor,
                                    Runnable task,
                                    T value)
Executes a Runnable task and returns a Future representing that task.

Parameters:
executor - the Executor to which the task will be submitted
task - the task to submit
value - the value which will become the return value of the task upon task completion
Returns:
a Future representing pending completion of the task
Throws:
RejectedExecutionException - if task cannot be scheduled for execution

execute

public static <T> Future<T> execute(Executor executor,
                                    Callable<T> task)
Executes a value-returning task and returns a Future representing the pending results of the task.

Parameters:
executor - the Executor to which the task will be submitted
task - the task to submit
Returns:
a Future representing pending completion of the task
Throws:
RejectedExecutionException - if task cannot be scheduled for execution

invoke

public static void invoke(Executor executor,
                          Runnable task)
                   throws ExecutionException,
                          InterruptedException
Executes a Runnable task and blocks until it completes normally or throws an exception.

Parameters:
executor - the Executor to which the task will be submitted
task - the task to submit
Throws:
RejectedExecutionException - if task cannot be scheduled for execution
ExecutionException - if the task encountered an exception while executing
InterruptedException

invoke

public static <T> T invoke(Executor executor,
                           Callable<T> task)
                    throws ExecutionException,
                           InterruptedException
Executes a value-returning task and blocks until it returns a value or throws an exception.

Parameters:
executor - the Executor to which the task will be submitted
task - the task to submit
Returns:
a Future representing pending completion of the task
Throws:
RejectedExecutionException - if task cannot be scheduled for execution
InterruptedException - if interrupted while waiting for completion
ExecutionException - if the task encountered an exception while executing

execute

public static Future<Object> execute(Executor executor,
                                     PrivilegedAction action)
Executes a privileged action under the current access control context and returns a Future representing the pending result object of that action.

Parameters:
executor - the Executor to which the task will be submitted
action - the action to submit
Returns:
a Future representing pending completion of the action
Throws:
RejectedExecutionException - if action cannot be scheduled for execution

execute

public static Future<Object> execute(Executor executor,
                                     PrivilegedExceptionAction action)
Executes a privileged exception action under the current access control context and returns a Future representing the pending result object of that action.

Parameters:
executor - the Executor to which the task will be submitted
action - the action to submit
Returns:
a Future representing pending completion of the action
Throws:
RejectedExecutionException - if action cannot be scheduled for execution

defaultThreadFactory

public static ThreadFactory defaultThreadFactory()
Return a default thread factory used to create new threads. This factory creates all new threads used by an Executor in the same ThreadGroup. If there is a SecurityManager, it uses the group of System.getSecurityManager(), else the group of the thread invoking this defaultThreadFactory method. Each new thread is created as a non-daemon thread with priority Thread.NORM_PRIORITY. New threads have names accessible via Thread.getName() of pool-N-thread-M, where N is the sequence number of this factory, and M is the sequence number of the thread created by this factory.

Returns:
the thread factory

privilegedThreadFactory

public static ThreadFactory privilegedThreadFactory()
Return a thread factory used to create new threads that have the same permissions as the current thread. This factory creates threads with the same settings as defaultThreadFactory(), additionally setting the AccessControlContext and contextClassLoader of new threads to be the same as the thread invoking this privilegedThreadFactory method. A new privilegedThreadFactory can be created within an AccessController.doPrivileged(java.security.PrivilegedAction) action setting the current thread's access control context to create threads with the selected permission settings holding within that action.

Note that while tasks running within such threads will have the same access control and class loader settings as the current thread, they need not have the same ThreadLocal or InheritableThreadLocal values. If necessary, particular values of thread locals can be set or reset before any task runs in ThreadPoolExecutor subclasses using ThreadPoolExecutor.beforeExecute(java.lang.Thread, java.lang.Runnable). Also, if it is necessary to initialize worker threads to have the same InheritableThreadLocal settings as some other designated thread, you can create a custom ThreadFactory in which that thread waits for and services requests to create others that will inherit its values.

Returns:
the thread factory
Throws:
AccessControlException - if the current access control context does not have permission to both get and set context class loader.
See Also:
PrivilegedFutureTask