首页 > 代码库 > Java基础之-ExecutorService

Java基础之-ExecutorService

翻译javadoc系列文章之:ExecutorService

/**

 * An {@link Executor} that provides methods to manage termination and

 * methods that can produce a {@link Future} for tracking progress of

 * one or more asynchronous tasks.

 *

 * <p> An <tt>ExecutorService</tt> can be shut down, which will cause

 * it to reject new tasks.  Two different methods are provided for

 * shutting down an <tt>ExecutorService</tt>. The {@link #shutdown}

 * method will allow previously submitted tasks to execute before

 * terminating, while the {@link #shutdownNow} method prevents waiting

 * tasks from starting and attempts to stop currently executing tasks.

 * Upon termination, an executor has no tasks actively executing, no

 * tasks awaiting execution, and no new tasks can be submitted.  An

 * unused <tt>ExecutorService</tt> should be shut down to allow

 * reclamation of its resources.

 *

 * <p> Method <tt>submit</tt> extends base method {@link

 * Executor#execute} by creating and returning a {@link Future} that

 * can be used to cancel execution and/or wait for completion.

 * Methods <tt>invokeAny</tt> and <tt>invokeAll</tt> 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 customized variants of these methods.)

 *

 * <p>The {@link Executors} class provides factory methods for the

 * executor services provided in this package.

 

ExecutorService 接口是一个提供管理终止(termination)和能够生成跟踪一个或多个异步任务进程结果的方法的接口。

介绍ExecutorService接口的功能:

  1:提供管理终止功能(拒绝新任务进入功能);

  2:提供方法来生成能够追踪一个或多个任务执行结果的Future;

 

一个ExecutorService可以被关闭,即使其拒绝接受新的任务。

它提供了两种不同的方法来关闭一个ExecutorService。

方法1(shutdown方法):允许之前已经提交的任务继续执行到结束。

方法2(shutdownNow方法):会阻止等待开始的

Java基础之-ExecutorService