Executors

Factory and utility methods for {@link Executor}, {@link ExecutorService}, {@link ScheduledExecutorService}, {@link ThreadFactory}, and {@link Callable} classes defined in this package. This class supports the following kinds of methods:

<ul> <li>Methods that create and return an {@link ExecutorService} set up with commonly useful configuration settings. <li>Methods that create and return a {@link ScheduledExecutorService} set up with commonly useful configuration settings. <li>Methods that create and return a "wrapped" ExecutorService, that disables reconfiguration by making implementation-specific methods inaccessible. <li>Methods that create and return a {@link ThreadFactory} that sets newly created threads to a known state. <li>Methods that create and return a {@link Callable} out of other closure-like forms, so they can be used in execution methods requiring {@code Callable}. </ul>

@author Doug Lea

Members

Static functions

callable
Callable!(void) callable(Runnable task)

Returns a {@link Callable} object that, when called, runs the given task and returns the given result. This can be useful when applying methods requiring a {@code Callable} to an otherwise resultless action. @param task the task to run @param result the result to return @param (T) the type of the result @return a callable object @throws NullPointerException if task null

callable
Callable!(T) callable(Runnable task, T result)
Undocumented in source. Be warned that the author may not have intended to support it.
defaultThreadFactory
ThreadFactory defaultThreadFactory()

Returns a default thread factory used to create new threads. This factory creates all new threads used by an Executor in the same {@link ThreadGroupEx}. If there is a {@link java.lang.SecurityManager}, it uses the group of {@link System#getSecurityManager}, else the group of the thread invoking this {@code defaultThreadFactory} method. Each new thread is created as a non-daemon thread with priority set to the smaller of {@code Thread.PRIORITY_DEFAULT} and the maximum priority permitted in the thread group. New threads have names accessible via {@link Thread#getName} of <em>pool-N-thread-M</em>, where <em>N</em> is the sequence number of this factory, and <em>M</em> is the sequence number of the thread created by this factory. @return a thread factory

invokeAll
List!(Future!(T)) invokeAll(ExecutorService es, Collection!(Callable!(T)) tasks, Duration timeout)

Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first. {@link Future#isDone} is {@code true} for each element of the returned list. Upon return, tasks that have not completed are cancelled. Note that a <em>completed</em> task could have terminated either normally or by throwing an exception. The results of this method are undefined if the given collection is modified while this operation is in progress.

invokeAll
List!(Future!(T)) invokeAll(ExecutorService es, Collection!(Callable!(T)) tasks)

Executes the given tasks, returning a list of Futures holding their status and results when all complete. {@link Future#isDone} is {@code true} for each element of the returned list. Note that a <em>completed</em> task could have terminated either normally or by throwing an exception. The results of this method are undefined if the given collection is modified while this operation is in progress.

invokeAny
T invokeAny(ExecutorService es, Collection!(Callable!(T)) tasks)

Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do. Upon normal or exceptional return, tasks that have not completed are cancelled. The results of this method are undefined if the given collection is modified while this operation is in progress.

invokeAny
T invokeAny(ExecutorService es, Collection!(Callable!(T)) tasks, Duration timeout)

Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do before the given timeout elapses. Upon normal or exceptional return, tasks that have not completed are cancelled. The results of this method are undefined if the given collection is modified while this operation is in progress.

newFixedThreadPool
ThreadPoolExecutor newFixedThreadPool(int nThreads)

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most {@code nThreads} threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. 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. The threads in the pool will exist until it is explicitly {@link ExecutorService#shutdown shutdown}.

newFixedThreadPool
ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory)

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue, using the provided ThreadFactory to create new threads when needed. At any point, at most {@code nThreads} threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. 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. The threads in the pool will exist until it is explicitly {@link ExecutorService#shutdown shutdown}.

newScheduledThreadPool
ScheduledExecutorService newScheduledThreadPool(int corePoolSize)

Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically. @param corePoolSize the number of threads to keep in the pool, even if they are idle @return the newly created scheduled thread pool @throws IllegalArgumentException if {@code corePoolSize < 0}

newScheduledThreadPool
ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory)

Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically. @param corePoolSize the number of threads to keep in the pool, even if they are idle @param threadFactory the factory to use when the executor creates a new thread @return the newly created scheduled thread pool @throws IllegalArgumentException if {@code corePoolSize < 0} @throws NullPointerException if threadFactory is null

newSingleThreadScheduledExecutor
ScheduledExecutorService newSingleThreadScheduledExecutor()

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 {@code newScheduledThreadPool(1)} the returned executor is guaranteed not to be reconfigurable to use additional threads.

newSingleThreadScheduledExecutor
ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory)

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 {@code newScheduledThreadPool(1, threadFactory)} the returned executor is guaranteed not to be reconfigurable to use additional threads.

submit
Future!(void) submit(ExecutorService es, Runnable task)

Submits a Runnable task for execution and returns a Future representing that task. The Future's {@code get} method will return {@code null} upon <em>successful</em> completion.

submit
Future!(T) submit(ExecutorService es, Runnable task, T result)

Submits a Runnable task for execution and returns a Future representing that task. The Future's {@code get} method will return the given result upon successful completion.

submit
Future!(T) submit(ExecutorService es, Callable!(T) task)

Submits a value-returning task for execution and returns a Future representing the pending results of the task. The Future's {@code get} method will return the task's result upon successful completion.

Meta