Interface ExecutorService Java Doc全解

基础知识

正文

相关内容

父接口:bash

子接口:网络

  • ScheduledExecutorService

实现类:异步

  • AbstractExecutorService
  • ForkJoinPool
  • ScheduledThreadPoolExecutor
  • ThreadPoolExecutor

概述

ExecutorService提供了一些方法,包括管理取消任务和可使用Future来追踪那些异步执行的任务结果。ExecutorService是能够被关闭的,固然这会致使它没法接受新的任务
ExecutorService提供了两个不一样的方法来关闭本身:socket

  • shutdown()方法容许以前提交的任务在本身关闭以前继续被执行
  • shutdownNow()方法会禁止等待中的任务,并且会尝试终止执行中的任务。

接近终止时,executor没有任何激活的任务,没有任何等待执行的任务,也不会有新的任务提交。一个再也不使用的ExecutorService应该被关闭,这样能够回收部分资源。post

submit方法调用了父接口的Executor.execute(Runnable)方法,建立了一个Future对象,能够用来取消或等待计算结束。invokeAny和invokeAll方法主要用于批量执行任务的场景,批量执行一批任务,分别等待至少一个任务结束或者所有任务结束。对于这些方法,ExecutorCompletionService 能够用来写一些自定义的变种。学习

Executors方法提供了一些工厂方法相关的工厂方法。测试

用例: 下面的例子使用线程池处理收到的请求,这实际上是一个简易的网络服务。它使用了预约义的工厂方法: Executors.newFixedThreadPool(int)ui

class NetworkService implements Runnable {
  private final ServerSocket serverSocket;
  private final ExecutorService pool;

  public NetworkService(int port, int poolSize)
      throws IOException {
    serverSocket = new ServerSocket(port);
    pool = Executors.newFixedThreadPool(poolSize);
  }

  public void run() { // run the service
    try {
      for (;;) {
        pool.execute(new Handler(serverSocket.accept()));
      }
    } catch (IOException ex) {
      pool.shutdown();
    }
  }
}

class Handler implements Runnable {
  private final Socket socket;
  Handler(Socket socket) { this.socket = socket; }
  public void run() {
    // read and service request on socket
  }
}
复制代码

以下的方法经过两步来关闭ExecutorService,第一步调用shutdown来拒收新的任务,而后调用shutdownNow,若是须要的话,取消全部处于等待中的任务:this

void shutdownAndAwaitTermination(ExecutorService pool) {
  pool.shutdown(); // Disable new tasks from being submitted
  try {
    // Wait a while for existing tasks to terminate
    if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
      pool.shutdownNow(); // Cancel currently executing tasks
      // Wait a while for tasks to respond to being cancelled
      if (!pool.awaitTermination(60, TimeUnit.SECONDS))
          System.err.println("Pool did not terminate");
    }
  } catch (InterruptedException ie) {
    // (Re-)Cancel if current thread also interrupted
    pool.shutdownNow();
    // Preserve interrupt status
    Thread.currentThread().interrupt();
  }
}
复制代码

内存一致性影响:在将Runnable或Callable任务提交给ExecutorService以前,线程中的操做发生在该任务执行的任何操做以前,而该操做又发生在经过Future.get()检索结果以前。spa

方法

boolean awaitTermination(long timeout, TimeUnit unit)

关闭本身以前会等待全部任务都执行完毕,此时方法会一直阻塞,直到阻塞时间大于超时时间,或者当前线程被中断

Parameters: timeout - 等待最大时长
unit - 等待时间单位

Returns:

  • executor正常关闭就返回true
  • 正常关闭以前到了超时时间

Throws:
InterruptedException - 执行时线程被中断

List<Future> invokeAll(Collection<? extends Callable> tasks)

执行给定的批量任务,当全部任务都执行完毕时会返回一个包含全部任务结果Future的list集合。集合中的全部元素的Future.isDone()方法的调用结果都是true。可是要注意,一个已完成的任务多是正常结束,也多是抛了异常。若是给定的任务集合在任务执行中被更改,可能会致使这个方法的返回值可能会变成未定义的。因此任务列表在提交到这个方法时不要作任何的修改!

Type Parameters:
T - the type of the values returned from the tasks

Parameters:
tasks - 任务列表

Returns:
一个Future集合用来表示任务的执行结果,集合顺序与给定的任务集合是一致的,并且集合中的future的状态都是已完成

Throws:
InterruptedException - 若是等待时被中断,这回致使未执行的任务会被取消
NullPointerException - 若是入参任务集合中的某个元素是null
RejectedExecutionException - 如何任意的任务没法被执行

List<Future> invokeAll(Collection<? extends Callable> tasks, long timeout, TimeUnit unit)

此方法和上面的方法基本是同样的,惟一的区别是:上面的方法执行完全部给定任务后才返回,此方法是执行全部任务,可是若是执行时间超过了超时时间,也会强制返回结果

Type Parameters:
T - the type of the values returned from the tasks

Parameters:
tasks - 任务列表
timeout - 超时时间
unit - 时间单位

Returns:
与上面的方法惟一的区别在于,若是执行批量任务没有报超时,那么返回结果的全部future都是已完成状态,不然会存在一些未完成的future

Throws: InterruptedException - 若是等待时被中断,这回致使未执行的任务会被取消
NullPointerException - 若是入参任务集合中的某个元素是null
RejectedExecutionException - 如何任意的任务没法被执行

T invokeAny(Collection<? extends Callable> tasks)

执行给定的任务,返回成功完成的某一个任务的结果,任意一个在超时时间以前完成的均可以。在接近返回时(包括正常返回和异常返回),那些没有完成的任务会被取消。若是给定的任务集合在任务执行中被更改,可能会致使这个方法的返回值可能会变成未定义的。因此任务列表在提交到这个方法时不要作任何的修改!

Type Parameters: T - the type of the values returned from the tasks

Parameters: tasks - 任务列表

Returns: 返回任务中的其中之一

Throws: InterruptedException - 若是等待时被中断,这回致使未执行的任务会被取消
NullPointerException - 若是入参任务集合中的某个元素是null
RejectedExecutionException - 如何任意的任务没法被执行 TimeoutException - 若是超时时间到了,可是没有任何一个任务完成 ExecutionException - 若是没有任务执行成功
IllegalArgumentException - 若是集合为空

T invokeAny(Collection<? extends Callable> tasks, long timeout, TimeUnit unit)

执行给定的任务,返回成功完成的某一个任务的结果,任意一个在超时时间以前完成的均可以。在接近返回时(包括正常返回和异常返回),那些没有完成的任务会被取消。若是给定的任务集合在任务执行中被更改,可能会致使这个方法的返回值可能会变成未定义的。因此任务列表在提交到这个方法时不要作任何的修改!

Type Parameters:
T - the type of the values returned from the tasks

Parameters:
tasks - 任务列表
timeout - 超时时间
unit - 时间单位

Returns:
返回任务中的其中之一

Throws: InterruptedException - 若是等待时被中断,这回致使未执行的任务会被取消
NullPointerException - 若是入参任务集合中的某个元素是null
RejectedExecutionException - 如何任意的任务没法被执行 TimeoutException - 若是超时时间到了,可是没有任何一个任务完成 ExecutionException - 若是没有任务执行成功

boolean isShutdown()

若是此executor已经被关闭,那么就返回true

Returns: 若是此executor已经被关闭,那么就返回true

boolean isTerminated()

若是关闭以前全部任务都已完成就返回true。这个方法在调用shutdown或shutdownNow以前永远都不会返回true。

Returns:
若是关闭以前全部任务都已完成就返回true。

void shutdown()

调用此方法后,以前提交的任务会被执行,可是任何新任务都不会被执行。当Executor已是关闭状态后再调用此方法不会任何附加的效果。这个方法不会等待历史提交的任务去完成执行。若是要那样请用awaitTermination。(这个描述不是很明朗,须要写测试类验证)

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 RuntimePermission("modifyThread"), or the security manager's checkAccess method denies access.

List shutdownNow()

尝试关闭全部激活的任务,当即中止那些正在等待中的任务,而后返回一个包含全部等待执行的任务列表。这个方法不会等待执行中的任务直到执行完毕,若是要那样请用awaitTermination。

Returns: 还没来得及执行的任务列表

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 RuntimePermission("modifyThread"), or the security manager's checkAccess method denies access.

Future submit(Callable task)

提交一个带返回值的任务(Callable)并且返回一个future对象来表明任务结果。若是任务执行成功,future.get方法会返回结果。 下面的代码可让你当即阻塞在等待任务完成的阶段:
result = exec.submit(aCallable).get();

下面的翻译还有待学习,学习完后会补充

Note: The Executors class includes a set of methods that can convert some other common closure-like objects, for example, PrivilegedAction to Callable form so they can be submitted.

Type Parameters:
T - the type of the task's result

Parameters: task - 等待执行的任务

Returns: 任务结果future

Throws: RejectedExecutionException - 若是任务没法被执行 NullPointerException - 若是参数为null

Future<?> submit(Runnable task)

提交一个Runnable任务,这个任务没有返回值,可是ExecutorService给它封装了一个Future,这个Future的get方法在任务执行成功后会返回一个null,也就是说,当你get到null的时候表明任务执行成功了

Parameters:
task - 任务

Returns:
任务结果future对象

Throws:
RejectedExecutionException - 任务没法被接受 NullPointerException - 参数为空

Future submit(Runnable task, T result)

提交一个Runnable任务,这个任务没有返回值,可是ExecutorService给它封装了一个Future,这个Future的get方法在任务执行成功后会返回你传进来的result,也就是说,当你get到result的时候表明任务执行成功了

Type Parameters:
T - the type of the result

Parameters:
task - 任务
result - 任务结果,在任务完成时future能够get到

Returns:
任务结果future对象

Throws: RejectedExecutionException - 任务没法被接受 NullPointerException - 参数为空

相关文章
相关标签/搜索