java.util.concurrent
Class Executors

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

public class Executors
extends java.lang.Object

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

Since:
1.5
Author:
Doug Lea

Method Summary
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 Cancellable execute(Executor executor, java.lang.Runnable task)
          Executes a Runnable task and returns a Cancellable representing that task.
static
<T> Future<T>
execute(Executor executor, java.lang.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, java.lang.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.
 
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,
                                  java.lang.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,
                                    java.lang.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,
                          java.lang.Runnable task)
                   throws ExecutionException,
                          java.lang.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
java.lang.InterruptedException

invoke

public static <T> T invoke(Executor executor,
                           Callable<T> task)
                    throws ExecutionException,
                           java.lang.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
java.lang.InterruptedException - if interrupted while waiting for completion
ExecutionException - if the task encountered an exception while executing