ExecutorService 接口

时间:2023-12-19 11:54:14

先看一个Executor接口,该接口只有一个方法:void execute(Runnable command),用于在未来某个时刻提交一个command,这个command可以被提交到一个新的线程,或者一个线程池,或者在调用线程中。

ExecutorService接口继承了Executor接口。主要是增加了shutDown、shutDownNow、invokeAll、invokeAny和submit方法。

  • invokeAny(Collection<? extends Callable<T>> tasks)
    提交所有任务,返回某个已完成的任务的结果 <T> T,有相应带超时版本。
  • invokeAll(Collection<? extends Callable<T>> tasks)
    提交所有任务,所有任务完成后,返回所有的结果 <T>List<Future<T>>,有相应带超时版本。
  • shutdown()
    执行器不再接受新的任务。
  • List<Runnable> shutDownNow()
    尝试结束所有正在执行的任务。
  • submit
    用于将任务提交给ExecutorService,会返回Future对象,用于查询执行状态。

下面是ExecutorService的源码:

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.
*/
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.
*/
List<Runnable> shutdownNow(); /**
* Returns <tt>true</tt> if this executor has been shut down.
*/
boolean isShutdown(); /**
* Returns <tt>true</tt> if all tasks have completed following shut down.
* Note that <tt>isTerminated</tt> is never <tt>true</tt> unless
* either <tt>shutdown</tt> or <tt>shutdownNow</tt> was called first.
*/
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.
*/
boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException; /**
* Submits a value-returning task for execution and returns a
* Future representing the pending results of the task. The
* Future's <tt>get</tt> method will return the task's result upon
* successful completion.
*/
<T> Future<T> submit(Callable<T> task); /**
* Submits a Runnable task for execution and returns a Future
* representing that task. The Future's <tt>get</tt> method will
* return the given result upon successful completion.
*/
<T> Future<T> submit(Runnable task, T result); /**
* Submits a Runnable task for execution and returns a Future
* representing that task. The Future's <tt>get</tt> method will
* return <tt>null</tt> upon <em>successful</em> completion.
*/
Future<?> submit(Runnable task); /**
* Executes the given tasks, returning a list of Futures holding
* their status and results when all complete.
*/
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException; /**
* 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 <tt>true</tt> for each
* element of the returned list.
* Upon return, tasks that have not completed are cancelled.
*/
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException; /**
* 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.
*/
<T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException, ExecutionException; /**
* 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.
*/
<T> T invokeAny(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}