public interface Executor {
void execute(Runnable command);
}
public interface ExecutorService extends Executor {
void shutdown(); // 已经提交的任务继续执行,不接受新的任务提交
List<Runnable> shutdownNow(); // 尝试终止正在执行的任务、取消正在等待执行的任务并返回它们
boolean isShutdown();
boolean isTerminated(); // 只有调用了shut down方法,并且没有任务在执行了,返回true
// 等待所有任务执行完毕,也需要调用古shut down方法,否则只能等待超时
boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException;
<T> Future<T> submit(Callable<T> task);
<T> Future<T> submit(Runnable task, T result); // result就是Future的get方法的返回值
Future<?> submit(Runnable task);
// 所有任务执行完毕,按序返回Future对象
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException;
// 所有任务执行完毕或超时,按序返回Future对象,超时任务返回的Future的isCancelled返回tue
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException;
// 返回第一个执行完毕的任务的Future对象,其余未执行完毕任务被取消
<T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException, ExecutionException;
// 返回第一个执行完毕的任务的Future对象,其余未执行完毕任务被取消
// 超时后没有任务完成则返回null
<T> T invokeAny(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
ExecutorService的几个方法
最新推荐文章于 2024-07-17 17:53:30 发布