Executor接口
/** * 执行提交的任务对象(Runnable) * Executor提供了一种将任务提交与任务运行方式的机制分离方法包括线程的使用、调用等细节 */ public interface Executor { /** * 在将来的某个时间执行给定的指令 * 指令可能运行在一个线程池的新线程中,也可能运行在调用线程中 * @throws RejectedExecutionException 如果任务无法被接受执行 */ void execute(Runnable command); }
ExecutorService接口
/** * 提供管理终止和生成Future来跟踪一个或多个一步任务的进度方法 * 内存一致性:happen-before(先行发生) */ public interface ExecutorService extends Executor { /** * 启动有序关闭:之前提交的任务被执行,但不会接受新的任务 * 如果已经关闭,额外调用没有影响 */ void shutdown(); /** * 尝试停止所有正在执行的任务,停止处理等待的任务,并返回等待执行的任务列表 * best-effort(尽力)尝试去终止执行中的任务,不给出任何保证,例如:典型的是通过:Thread.interrupt来实现取消,因此任务如果不响应中断可能永远不会终止 * @return 返回未执行的任务列表 */ List<Runnable> shutdownNow(); /** * 返回当前executor是否已经关闭 */ boolean isShutdown(); /** * shut down 之后所有的任务都已经完成 * 除非先调用shutDown()或者shutDownNow()否则不会返回true*/ boolean isTerminated(); /** * 阻塞直到以下事件之一先发生: * 1.所有任务都执行完成 返回:true * 2.等待超时 返回:false * 3.当前线程被中断 throws:InterruptedException */ boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException; /** * 提交有结果的任务,并返回一个Future代表计算结果的task * T result = executor.submit(Callable).get() 来阻塞等待结果 * Executors包含一些方法可以转换其他类似closure-like (类似闭包)对象 到 可以提交执行 * throws:RejectedExecutionException 如果任务无法被安排执行*/ <T> Future<T> submit(Callable<T> task); /** * 提交Runnable任务,并返回代表计算结果的Future,计算完成将返回给定结果 * RejectedExecutionException*/ <T> Future<T> submit(Runnable task, T result); /** * 提交Runnable任务并执行,返回代表计算结果的Future,计算成功完成时等到Null结果 * RejectedExecutionException*/ Future<?> submit(Runnable task); /** * 执行给定的task集合,并且返回一个Future列表持有状态和结果 当每个任务都完成(Future.isDone == ture)时 * 已完成:正常完成或者异常结束 * 如果任务执行时修改给定的任务集合,结果将未知 * Future列表以给定任务列表集合迭代器相同的顺序生成 * InterruptedException 当等待时被中断,将导致所有未完成任务被取消*/ <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException; /** * 执行给定任务集合,返回Future列表持有任务状态和结果 * 执行返回:先发生为准,返回时未执行结束的任务将被取消 * 1.所有任务执行结果 * 2.超时 * @see invokeAll*/ <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException; /** * 执行给定任务集合,返回一个已经执行完成的任务(不抛异常) * 方法返回时,所有未完成的任务将被取消 * see invokeAll*/ <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException; /** * 执行给定的任务集合,返回一个执行完成的任务结果(不抛异常)在超时之前 * 方法正常退出或异常退出时所有未完成的任务都将被取消 * TimeoutException 任何任务返回前超过了给定的时间 * @see invokeAny
*/ <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; }
Runnable接口
/** * 任何意在由线程执行的类都应该实现此接口 * Runnable接口旨在为希望在活动时执行代码的对象提供通用协议,如:Thread处于活动状态表示线程已经启动且尚未停止 * Runnable提供了active方法而非进程Thread,如果没有打算修改或增强类的基本行为,应该使用Runnable接口 * @since 1.0 */ @FunctionalInterface public interface Runnable { /** * 当接口的实现被用来创建thread 实例,启动thread会使单独运行的线程执行run()方法 */ public abstract void run(); }
Future接口
/** * 表示异步计算的结果。提供方法以检查计算是否完成,等待其完成和检索计算结果 * * 内存一致性影响:happen-before(先行发生) */ public interface Future<V> { /** * 尝试取消一个任务. This attempt will * 如果任务:已经完成、已经被取消、或者由于其他原因无法取消 那么尝试将失败 * 如果任务尚未启动,那么任务将不会再启动;如果任务已经启动,那么mayInterruplfRunning参数去定是否中断执此任务的线程 * cancel()方法执行返回后,isDone()将一直返回true;isCancelled()方法返回与cancel()一致 * {@code mayInterruptIfRunning} 执行此任务的线程是否被打断,否则允许任务执行结束 */ boolean cancel(boolean mayInterruptIfRunning); /** * 返回true如果任务在正常结束前被取消 */ boolean isCancelled(); /** * 返回true如果任务已经完成 * 完成可能是:1:正常结束、2:异常结束、3:被取消 */ boolean isDone(); /** * 如果需要,则等待计算结果 * CancellationException 任务被取消 * ExecutionException 计算出现异常 * InterruptedException 等待计算时当前线程被中断 */ V get() throws InterruptedException, ExecutionException; /** * 如果需要,最多在给定时间内等待计算 * TimeoutException 等待超时 * 其他见get()方法 */ V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; }