static
<T>
Future
|
static
ExecutorService
|
newFixedThreadPool
(int nThreads,
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
ScheduledExecutorService
|
newScheduledThreadPool
(int corePoolSize)
Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.
|
static
ScheduledExecutorService
|
newScheduledThreadPool
(int corePoolSize,
ThreadFactory
Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.
|
static
ExecutorService
|
newSingleThreadExecutor
()
Creates an Executor that uses a single worker thread operating off an unbounded queue.
|
static
ExecutorService
|
newSingleThreadExecutor
(
ThreadFactory
Creates an Executor that uses a single worker thread operating off an unbounded queue, and uses the provided ThreadFactory to create a new thread when needed.
|
static
ScheduledExecutorService
|
newSingleThreadScheduledExecutor
()
Creates a single-threaded executor that can schedule commands to run after a given delay, or to execute periodically.
|
static
ScheduledExecutorService
|
newSingleThreadScheduledExecutor
(
ThreadFactory
Creates a single-threaded executor that can schedule commands to run after a given delay, or to execute periodically.
|
static
|
privilegedCallable
execute
(
Callable
Executor
<T> callable)
executor,
Runnable
task, T value)
Returns a
Callable
object that will, when called, execute the given
callable
under the current access control context.
Executes a Runnable task and returns a Future representing that task.
|
static
|
privilegedCallableUsingCurrentClassLoader
invoke
(
Callable
Executor
<T> callable)
executor,
Callable
<T> task)
Returns a
Callable
object that will, when called, execute the given
callable
under the current access control context, with the current context class loader as the context class loader.
Executes a value-returning task and blocks until it returns a value or throws an exception.
|
static
ThreadFactory
|
privilegedThreadFactory
()
Returns a thread factory used to create new threads that have the same permissions as the current thread.
|
static void
|
invoke
(
Executor
executor,
Runnable
Executes a Runnable task and blocks until it completes normally or throws an exception.
|
static
ExecutorService
|
unconfigurableExecutorService
(
ExecutorService
Returns an object that delegates all defined
ExecutorService
|
static
ExecutorService
|
newCachedThreadPool
()
Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.
|
static
ScheduledExecutorService
|
unconfigurableScheduledExecutorService
(
ScheduledExecutorService
Returns an object that delegates all defined
ScheduledExecutorService
|
static
ExecutorService
|
newCachedThreadPool
(
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
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
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
|
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.
Unlike the otherwise equivalent
This method is equivalent in effect to
newFixedThreadPool(1)
new FixedThreadPool(1)
the returned executor is guaranteed not to be reconfigurable to use additional threads.
.
-
-
Returns:
-
the
newly created
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
a
new
thread
threads
when needed.
Unlike the otherwise equivalent
newFixedThreadPool(1, threadFactory)
the returned executor is guaranteed not to be reconfigurable to use additional threads.
-
-
Parameters:
-
threadFactory - the factory to use when creating new threads
-
Returns:
-
the
newly created
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
newSingleThreadScheduledExecutor
execute
public static ScheduledExecutorService Cancellable newSingleThreadScheduledExecutor  execute ()  (  Executor executor,
 Runnable task)
-
Creates a single-threaded executor that can schedule commands to run after a given delay, or to execute periodically. (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. Unlike the otherwise equivalent
newScheduledThreadPool(1)
the returned executor is guaranteed not to be reconfigurable to use additional threads.
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:
-
the newly created scheduled executor
a Cancellable representing pending completion of the task
-
Throws:
-
RejectedExecutionException
- if task cannot be scheduled for execution
newSingleThreadScheduledExecutor
execute
public static ScheduledExecutorService <T>  Future <T> newSingleThreadScheduledExecutor  execute ( ThreadFactory Executor threadFactory)  executor,
 Runnable task,
T value)
-
Creates a single-threaded executor that can schedule commands to run after a given delay, or to execute periodically. (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. Unlike the otherwise equivalent
newScheduledThreadPool(1, threadFactory)
the returned executor is guaranteed not to be reconfigurable to use additional threads.
Executes a Runnable task and returns a Future representing that task.
-
-
Parameters:
-
threadFactory - the factory to use when creating new threads
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 newly created scheduled executor
a Future representing pending completion of the task
-
Throws:
-
RejectedExecutionException
- if task cannot be scheduled for execution
newScheduledThreadPool
execute
public static ScheduledExecutorService <T>  Future <T> newScheduledThreadPool  execute (int corePoolSize)  (  Executor executor,
 Callable <T> task)
-
Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.
Executes a value-returning task and returns a Future representing the pending results of the task.
-
-
Parameters:
-
corePoolSize - the number of threads to keep in the pool, even if they are idle.
executor - the Executor to which the task will be submitted
-
task - the task to submit
-
Returns:
-
a newly created scheduled thread pool
a Future representing pending completion of the task
-
Throws:
-
RejectedExecutionException
- if task cannot be scheduled for execution
newScheduledThreadPool
invoke
public static ScheduledExecutorService void newScheduledThreadPool  invoke (int corePoolSize,
ThreadFactory (  Executor threadFactory)  executor,
 Runnable task)
throws  ExecutionException ,
 InterruptedException
-
Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.
Executes a Runnable task and blocks until it completes normally or throws an exception.
-
-
Parameters:
-
corePoolSize - the number of threads to keep in the pool, even if they are idle.
executor - the Executor to which the task will be submitted
-
threadFactory - the factory to use when the executor creates a new thread.
task - the task to submit
-
Returns:
Throws:
-
a newly created scheduled thread pool
RejectedExecutionException
- if task cannot be scheduled for execution
-
ExecutionException
- if the task encountered an exception while executing
-
InterruptedException
unconfigurableExecutorService
invoke
public static ExecutorService <T> T unconfigurableExecutorService  invoke ( ExecutorService Executor executor)  executor,
 Callable <T> task)
throws  ExecutionException ,
 InterruptedException
-
Returns an object that delegates all defined
ExecutorService
methods to the given executor, but not any other methods that might otherwise be accessible using casts. This provides a way to safely "freeze" configuration and disallow tuning of a given concrete implementation.
Executes a value-returning task and blocks until it returns a value or throws an exception.
-
-
Parameters:
-
executor - the underlying implementation
executor - the Executor to which the task will be submitted
-
task - the task to submit
-
Returns:
-
an
ExecutorService
instance
a Future representing pending completion of the task
-
Throws:
-
NullPointerException
RejectedExecutionException
- if executor null
- if task cannot be scheduled for execution
-
InterruptedException
- if interrupted while waiting for completion
-
ExecutionException
- if the task encountered an exception while executing
unconfigurableScheduledExecutorService
execute
public static ScheduledExecutorService Future <  Object > unconfigurableScheduledExecutorService  execute ( ScheduledExecutorService Executor executor)  executor,
 PrivilegedAction action)
-
Returns an object that delegates all defined
ScheduledExecutorService
methods to the given executor, but not any other methods that might otherwise be accessible using casts. This provides a way to safely "freeze" configuration and disallow tuning of a given concrete implementation.
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 underlying implementation
executor - the Executor to which the task will be submitted
-
action - the action to submit
-
Returns:
-
a
ScheduledExecutorService
instance
a Future representing pending completion of the action
-
Throws:
-
NullPointerException
RejectedExecutionException
- if executor null
- if action cannot be scheduled for execution
defaultThreadFactory
execute
public static ThreadFactory Future <  Object > defaultThreadFactory  execute ()  (  Executor executor,
 PrivilegedExceptionAction action)
-
Returns 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.
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 thread factory
a Future representing pending completion of the action
-
Throws:
-
RejectedExecutionException
- if action cannot be scheduled for execution
privilegedThreadFactory
defaultThreadFactory
public static ThreadFactory privilegedThreadFactory  defaultThreadFactory ()
-
Returns
Return
a
default
thread factory used to create new
threads that have the same permissions as the current thread.
threads.
This factory creates
all new
threads
with
used by an Executor in
the same
settings as
defaultThreadFactory()
ThreadGroup
, additionally setting the AccessControlContext and contextClassLoader of new threads to be the same as the thread invoking this
. If there is a
SecurityManager
, it uses the group of
System.getSecurityManager()
, else the group of the thread invoking this
privilegedThreadFactory
defaultThreadFactory
method.
A new
Each new thread is created as a non-daemon thread with priority
privilegedThreadFactory
Thread.NORM_PRIORITY
can be created within an
AccessController.doPrivileged(java.security.PrivilegedAction
)
. New threads have names accessible via
Thread.getName()
action setting the current thread's access control context to create threads with the selected permission settings holding within that action.
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.
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:
-
a thread factory
-
Throws:
-
AccessControlException
- if the current access control context does not have permission to both get and set context class loader.
the thread factory
callable
privilegedThreadFactory
public static <T> Callable ThreadFactory <T> callable  privilegedThreadFactory ( Runnable task,
T result)  ()
-
Returns a
Callable
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()
object that, when called, runs the given task and returns the given result. This can be useful when applying methods requiring a
, additionally setting the AccessControlContext and contextClassLoader of new threads to be the same as the thread invoking this
Callable
privilegedThreadFactory
to an otherwise resultless action.
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.
-
-
Parameters:
Returns:
-
task - the task to run
-
result - the result to return
the thread factory
-
Returns:
Throws:
-
a callable object
AccessControlException - if the current access control context does not have permission to both get and set context class loader.
-
Throws:
See Also:
-
NullPointerException
PrivilegedFutureTask
- if task null
callable
public static Callable < Object > callable ( Runnable task)
-
Returns a
Callable
object that, when called, runs the given task and returns
null
.
-
-
Parameters:
-
task - the task to run
-
Returns:
-
a callable object
-
Throws:
-
NullPointerException
- if task null
callable
public static Callable < Object > callable ( PrivilegedAction action)
-
Returns a
Callable
object that, when called, runs the given privileged action and returns its result.
-
-
Parameters:
-
action - the privileged action to run
-
Returns:
-
a callable object
-
Throws:
-
NullPointerException
- if action null
callable
public static Callable < Object > callable ( PrivilegedExceptionAction action)
-
Returns a
Callable
object that, when called, runs the given privileged exception action and returns its result.
-
-
Parameters:
-
action - the privileged exception action to run
-
Returns:
-
a callable object
-
Throws:
-
NullPointerException
- if action null
privilegedCallable
public static <T> Callable <T> privilegedCallable ( Callable <T> callable)
-
Returns a
Callable
object that will, when called, execute the given
callable
under the current access control context. This method should normally be invoked within an
AccessController.doPrivileged(java.security.PrivilegedAction
)
action to create callables that will, if possible, execute under the selected permission settings holding within that action; or if not possible, throw an associated
AccessControlException
.
-
-
Parameters:
-
callable - the underlying task
-
Returns:
-
a callable object
-
Throws:
-
NullPointerException
- if callable null
privilegedCallableUsingCurrentClassLoader
public static <T> Callable <T> privilegedCallableUsingCurrentClassLoader ( Callable <T> callable)
-
Returns a
Callable
object that will, when called, execute the given
callable
under the current access control context, with the current context class loader as the context class loader. This method should normally be invoked within an
AccessController.doPrivileged(java.security.PrivilegedAction
)
action to create callables that will, if possible, execute under the selected permission settings holding within that action; or if not possible, throw an associated
AccessControlException
.
-
-
Parameters:
-
callable - the underlying task
-
Returns:
-
a callable object
-
Throws:
-
NullPointerException
- if callable null
-
AccessControlException
- if the current access control context does not have permission to both set and get context class loader.
|