JDK中ExecutorService源码解读

下面是对ExecutorService源码的翻译,可以很好的理解该类的作用

package lucky;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;

/**
 * @Author:chaoqiang.zhou
 * @Description:
 * @Date:Create in 11:01 2017/10/19
 */
public interface ExecutorService extends Executor {

    /**
     *在之前提交的,需要被执行的任务中,有序的进行关闭操作,并且此时不会再接受新的任务
     * 如果此时所有的任务已经关闭的话,那么就不会起到什么效果,因为已经没有任务可关闭了
     */
    void shutdown();


    /**
     * 企图关闭所有正在执行的任务,并且中断正在等待要执行的任务,返回一个包含正在等待的任务的列表
     * @return
     */
    List<Runnable> shutdownNow();

    /**
     * Returns {@code true} if this executor has been shut down.
     *
     * @return {@code true} if this executor has been shut down
     */
    /**
     * 如果线程已经关闭了,就返回true
     * @return
     */
    boolean isShutdown();

    /**
     * 如果所有的线程任务已经关闭了,就返回true
     * @return
     */
    boolean isTerminated();


    /**
     * 只有当所有的任务都成功执行,否则会一直处于阻塞状态,只有当一下情况发生时,才会中断阻塞
     * 例如收到一个关闭的请求,或者超时发生、或者当前的线程被中断后
     * @param timeout
     * @param unit
     * @return
     * @throws InterruptedException
     */
    boolean awaitTermination(long timeout, TimeUnit unit)
            throws InterruptedException;


    /**
     * 提交一个需要返回结果的任务去执行,返回一个有结果的消息体,只有成功执行后,才会返回结果
     * @param task
     * @param <T>
     * @return
     */
    <T> Future<T> submit(Callable<T> task);


    /**
     * 只有当任务成功被执行后,才会返回给定的结果
     * @param task
     * @param result
     * @param <T>
     * @return
     */
    <T> Future<T> submit(Runnable task, T result);


    Future<?> submit(Runnable task);


    /**
     * 提交一批任务,并返回一批任务的结果列表
     * @param tasks
     * @param <T>
     * @return
     * @throws InterruptedException
     */
    <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;


    /**
     * 提交一批任务信息,当其中一个成功的执行,没有返回异常的时候,就返回结果
     * @param tasks
     * @param <T>
     * @return
     * @throws InterruptedException
     * @throws ExecutionException
     */
    <T> T invokeAny(Collection<? extends Callable<T>> tasks)
            throws InterruptedException, ExecutionException;


    <T> T invokeAny(Collection<? extends Callable<T>> tasks,
                    long timeout, TimeUnit unit)
            throws InterruptedException, ExecutionException, TimeoutException;
}

猜你喜欢

转载自blog.csdn.net/zhouchaoqiang/article/details/78282462
今日推荐