java并发编程学习之线程池Executor和ExecutorService一

4次阅读

共计 1150 个字符,预计需要花费 3 分钟才能阅读完成。

Executor 接口

void execute(Runnable command)// 用于提交 command 任务 

ExecutorService 接口

继承了 Executor 接口

// 设置线程的状态,还没执行的线程会被中断
void shutdown();
// 设置线程的状态,尝试停止正在进行的线程
List<Runnable> shutdownNow();
// 当调用 shutdown() 或 shutdownNow() 方法后返回为 true
boolean isShutdown();
// 当调用 shutdown() 方法后,并且所有提交的任务完成后返回为 true
// 当调用 shutdownNow() 方法后,成功停止后返回为 true;
boolean isTerminated();
// 当前线程阻塞,直到线程执行完、时间到、被中断
boolean awaitTermination(long timeout, TimeUnit unit)
    throws InterruptedException;
// 提交一个 Callable 任务
<T> Future<T> submit(Callable<T> task);
// 提交一个 Runnable 任务,因为 Runnable 没有返回指,所以第二个参数是用来返回值
<T> Future<T> submit(Runnable task, T result);
// 提交一个 Runnable 任务
Future<?> submit(Runnable task);
// 执行所有任务
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
    throws InterruptedException;
// 执行所有任务,有过期时间
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
                              long timeout, TimeUnit unit)
    throws InterruptedException;
// 有一个任务结束就可以返回
<T> T invokeAny(Collection<? extends Callable<T>> tasks,
                long timeout, TimeUnit unit)
    throws InterruptedException, ExecutionException, TimeoutException;
// 有一个任务结束就可以返回,有过期时间
<T> T invokeAny(Collection<? extends Callable<T>> tasks,
                    long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;

正文完
 0