ExecutorService的几个方法

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值