/*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain. Use, modify, and
* redistribute this code in any way without acknowledgement.
*/
package java.util.concurrent;
import java.util.List;
import java.util.Collection;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
/**
* An Executor that provides methods to manage termination
* and methods that can produce a {@link Future} for tracking
* progress of one or more asynchronous tasks.
*
*
* An ExecutorService can be shut down, which will cause it
* to stop accepting new tasks. After being shut down, the executor
* will eventually terminate, at which point no tasks are actively
* executing, no tasks are awaiting execution, and no new tasks can be
* submitted.
*
*
Method submit and related methods extend base method
* execute by creating and returning a {@link Future} that
* can be used to cancel execution and/or wait for completion.
* Methods invokeAny and invokeAll perform the most
* commonly useful forms of bulk execution, executing a collection of
* tasks and then waiting for at least one, or all to complete. (Class
* {@link ExecutorCompletionService} can be used to write customizable
* versions of such methods.)
*
*
The {@link Executors} class provides factory methods for the
* executor services provided in this package.
*
* @since 1.5
* @author Doug Lea
*/
public interface ExecutorService extends Executor {
/**
* Initiates an orderly shutdown in which previously submitted
* tasks are executed, but no new tasks will be
* accepted. Invocation has no additional effect if already shut
* down.
* @throws SecurityException if a security manager exists and
* shutting down this ExecutorService may manipulate threads that
* the caller is not permitted to modify because it does not hold
* {@link java.lang.RuntimePermission}("modifyThread"),
* or the security manager's checkAccess method denies access.
*/
void shutdown();
/**
* Attempts to stop all actively executing tasks, halts the
* processing of waiting tasks, and returns a list of the tasks that were
* awaiting execution.
*
*
There are no guarantees beyond best-effort attempts to stop
* processing actively executing tasks. For example, typical
* implementations will cancel via {@link Thread#interrupt}, so if any
* tasks mask or fail to respond to interrupts, they may never terminate.
*
* @return list of tasks that never commenced execution
* @throws SecurityException if a security manager exists and
* shutting down this ExecutorService may manipulate threads that
* the caller is not permitted to modify because it does not hold
* {@link java.lang.RuntimePermission}("modifyThread"),
* or the security manager's checkAccess method denies access.
*/
List shutdownNow();
/**
* Returns true if this executor has been shut down.
*
* @return true if this executor has been shut down
*/
boolean isShutdown();
/**
* Returns true if all tasks have completed following shut down.
* Note that isTerminated is never true unless
* either shutdown or shutdownNow was called first.
*
* @return true if all tasks have completed following shut down
*/
boolean isTerminated();
/**
* Blocks until all tasks have completed execution after a shutdown
* request, or the timeout occurs, or the current thread is
* interrupted, whichever happens first.
*
* @param timeout the maximum time to wait
* @param unit the time unit of the timeout argument
* @return true if this executor terminated and false
* if the timeout elapsed before termination
* @throws InterruptedException if interrupted while waiting
*/
boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException;
/**
* Submits a Runnable task for execution and returns a Future
* representing that task.
*
* @param task the task to submit
* @param result the result to return upon completion
* If you do not need a particular result, consider using
* the form: Future<?> cancellationHandle = e.submit(task, null).
* @return a Future representing pending completion of the task,
* and whose get() method will return the given value
* upon completion
* @throws RejectedExecutionException if task cannot be scheduled
* for execution
*/
Future submit(Runnable task, T result);
/**
* Submits a value-returning task for execution and returns a Future
* representing the pending results of the task.
*
* @param task the task to submit
* @return a Future representing pending completion of the task
* @throws RejectedExecutionException if task cannot be scheduled
* for execution
*/
Future submit(Callable task);
/**
* Submits a privileged action for execution under the current
* access control context and returns a Future representing the
* pending result object of that action.
*
* @param action the action to submit
* @return a Future representing pending completion of the action
* @throws RejectedExecutionException if action cannot be scheduled
* for execution
*/
Future