java.lang.Object java.util.concurrent.Executors
Factory and utility methods for Executor
, ExecutorService
, ThreadFactory
, Future
, and Cancellable
classes defined in this package.
Method Summary | ||
---|---|---|
static ThreadFactory |
defaultThreadFactory()
Return a default thread factory used to create new threads. |
|
static
|
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
|
execute(Executor executor,
Runnable task,
T value)
Executes a Runnable task and returns a Future representing that task. |
|
static
|
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 |
---|
public static ExecutorService newFixedThreadPool(int nThreads)
nThreads
- the number of threads in the pool
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory)
nThreads
- the number of threads in the poolthreadFactory
- the factory to use when creating new threads
public static ExecutorService newSingleThreadExecutor()
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory)
threadFactory
- the factory to use when creating new
threads
public static ExecutorService newCachedThreadPool()
ThreadPoolExecutor
constructors.
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory)
threadFactory
- the factory to use when creating new threads
public static Cancellable execute(Executor executor, Runnable task)
executor
- the Executor to which the task will be submittedtask
- the task to submit
RejectedExecutionException
- if task cannot be scheduled
for executionpublic static <T> Future<T> execute(Executor executor, Runnable task, T value)
executor
- the Executor to which the task will be submittedtask
- the task to submitvalue
- the value which will become the return value of
the task upon task completion
RejectedExecutionException
- if task cannot be scheduled
for executionpublic static <T> Future<T> execute(Executor executor, Callable<T> task)
executor
- the Executor to which the task will be submittedtask
- the task to submit
RejectedExecutionException
- if task cannot be scheduled
for executionpublic static void invoke(Executor executor, Runnable task) throws ExecutionException, InterruptedException
executor
- the Executor to which the task will be submittedtask
- the task to submit
RejectedExecutionException
- if task cannot be scheduled
for execution
ExecutionException
- if the task encountered an exception
while executing
InterruptedException
public static <T> T invoke(Executor executor, Callable<T> task) throws ExecutionException, InterruptedException
executor
- the Executor to which the task will be submittedtask
- the task to submit
RejectedExecutionException
- if task cannot be scheduled
for execution
InterruptedException
- if interrupted while waiting for
completion
ExecutionException
- if the task encountered an exception
while executingpublic static Future<Object> execute(Executor executor, PrivilegedAction action)
executor
- the Executor to which the task will be submittedaction
- the action to submit
RejectedExecutionException
- if action cannot be scheduled
for executionpublic static Future<Object> execute(Executor executor, PrivilegedExceptionAction action)
executor
- the Executor to which the task will be submittedaction
- the action to submit
RejectedExecutionException
- if action cannot be scheduled
for executionpublic static ThreadFactory defaultThreadFactory()
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.
public static ThreadFactory privilegedThreadFactory()
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.
AccessControlException
- if the current access control
context does not have permission to both get and set context
class loader.PrivilegedFutureTask