手撕源码系列之线程池 -- 线程池基础

线程池的关系图谱

线程池基础

Executor接口

Executor接口作为java在1.5版本提供的线程池核心接口,他的只有一个方法execute(Runnable command)
public interface Executor {
    
    

    /**
     * Executes the given command at some time in the future.  The command
     * may execute in a new thread, in a pooled thread, or in the calling
     * thread, at the discretion of the {@code Executor} implementation.
     * 在将来执行给定的任务(即Runable),
     * 该任务可能会由一个新线程执行,也有可能由线程池内的线程执行,或者由一个正在被调用的线程中执行
     * 具体情况由实现类自行决定
     * 当Runnable命令被拒绝时会抛出异常RejectedExecutionException
     *
     * @param command the runnable task
     * @throws RejectedExecutionException if this task cannot be
     * accepted for execution
     * @throws NullPointerException if command is null
     */
    void execute(Runnable command);

ExecutorService

ExecutorService接口 继承了 Executor接口 额外添加了一些操作方法

public interface ExecutorService extends Executor {
    
    

    /**
     * 顺序关闭,执行以前提交的任务,但不接受新任务
     */
    void shutdown();

    /**
     * 停止所有正在执行的活动任务,暂停处理正在等待的任务,并返回等待执行的任务列表
     */
    List<Runnable> shutdownNow();

    /**
     * 判断线程池是否关闭(即Shutdwon状态)
     */
    boolean isShutdown();

    /**
     * 判断线程关闭后,所有任务是否已完成
     */
    boolean isTerminated();

    /**
     * 线程池关闭、超时或者线程中断,以上发生任意情况都将导致阻塞,直到所有任务完成执行
     */
    boolean awaitTermination(long timeout, TimeUnit unit)
        throws InterruptedException;

    /**
     * 提交一个返回值的任务用于执行,返回一个表示任务的未决结果的Future
     */
    <T> Future<T> submit(Callable<T> task);

    /**
     * 提交一个Runnable任务用于执行,并返回一个表示该任务的Future
     */
    <T> Future<T> submit(Runnable task, T result);

    /**
     * 提交一个Runnable任务用于执行,并返回一个表示该任务的Future
     */
    Future<?> submit(Runnable task);

    /**
     * 执行给定的任务,当所有任务完成时,返回保持任务状态和结果的Future列表
     */
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
        throws InterruptedException;

    /**
     * 执行给定的任务,当所有任务完成或超时期满时(无论哪个首先发生),返回保持任务状态和结果的Future列表
     */
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
                                  long timeout, TimeUnit unit)
        throws InterruptedException;

    /**
     * 执行给定的任务,如果某个任务已成功完成(也就是未抛出异常),则返回其结果
     */
    <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;
}

ScheduledExecutorService

ScheduledExecutorService接口是用于声明定时调度任务的接口方法,
继承的是ExecutorService方法
主要有四种方法:

public interface ScheduledExecutorService extends ExecutorService {
    
    

    //创建和实现一个延时调度任务,在给定的delay时间之后执行
    public ScheduledFuture<?> schedule(Runnable command,
                                       long delay, TimeUnit unit);

    //创建延迟调度任务,会在delay时间之后开始执行任务
    public <V> ScheduledFuture<V> schedule(Callable<V> callable,
                                           long delay, TimeUnit unit);

    //创建延迟周期性调度任务,第一次任务会在initialDelay时刻执行,
    //然后每次执行完看执行耗时是否达到period时间段,
    //如果达到就直接执行下一次任务,如果小于就等达到period再执行下一次任务
    public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                                  long initialDelay,
                                                  long period,
                                                  TimeUnit unit);

    //创建延迟调度任务,第一次任务在initialDelay时刻执行,然后每隔delay时间执行下一次任务
    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                     long initialDelay,
                                                     long delay,
                                                     TimeUnit unit);

}

AbstractExecutorService

AbstractExecutorService 是一个继承了ExecutorService接口的 抽象类
是实现线程池的重要类,java内部提供的线程池均继承了这个类。

public abstract class AbstractExecutorService implements ExecutorService {
    
    

    //通过给定的Runnable和默认结果值返回一个RunnableFuture对象
    protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
    
    
        return new FutureTask<T>(runnable, value);
    }

    //通过给定的Runnable返回一个RunnableFuture对象
    protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
    
    
        return new FutureTask<T>(callable);
    }

    //将传入的Runnable包装为一个RunnableFuture对象,交由execute(Runnable)方法处理
    public Future<?> submit(Runnable task) {
    
    
        if (task == null) throw new NullPointerException();
        RunnableFuture<Void> ftask = newTaskFor(task, null);
        execute(ftask);
        return ftask;
    }

    //将传入的Runnable和默认结果值包装为一个RunnableFuture对象,交由execute(Runnable)方法处理
    public <T> Future<T> submit(Runnable task, T result) {
    
    
        if (task == null) throw new NullPointerException();
        RunnableFuture<T> ftask = newTaskFor(task, result);
        execute(ftask);
        return ftask;
    }

    //将传入的Callable包装为一个RunnableFuture对象,交由execute(Runnable)方法处理
    public <T> Future<T> submit(Callable<T> task) {
    
    
        if (task == null) throw new NullPointerException();
        RunnableFuture<T> ftask = newTaskFor(task);
        execute(ftask);
        return ftask;
    }

    /**
     * the main mechanics of invokeAny.
     */
    private <T> T doInvokeAny(Collection<? extends Callable<T>> tasks,
                              boolean timed, long nanos)
        throws InterruptedException, ExecutionException, TimeoutException {
    
    
        if (tasks == null)
            throw new NullPointerException();
        int ntasks = tasks.size();
        if (ntasks == 0)
            throw new IllegalArgumentException();
        ArrayList<Future<T>> futures = new ArrayList<Future<T>>(ntasks);
        ExecutorCompletionService<T> ecs =
            new ExecutorCompletionService<T>(this);

        // For efficiency, especially in executors with limited
        // parallelism, check to see if previously submitted tasks are
        // done before submitting more of them. This interleaving
        // plus the exception mechanics account for messiness of main
        // loop.

        try {
    
    
            // Record exceptions so that if we fail to obtain any
            // result, we can throw the last exception we got.
            ExecutionException ee = null;
            final long deadline = timed ? System.nanoTime() + nanos : 0L;
            Iterator<? extends Callable<T>> it = tasks.iterator();

            // Start one task for sure; the rest incrementally
            futures.add(ecs.submit(it.next()));
            --ntasks;
            int active = 1;

            for (;;) {
    
    
                Future<T> f = ecs.poll();
                if (f == null) {
    
    
                    if (ntasks > 0) {
    
    
                        --ntasks;
                        futures.add(ecs.submit(it.next()));
                        ++active;
                    }
                    else if (active == 0)
                        break;
                    else if (timed) {
    
    
                        f = ecs.poll(nanos, TimeUnit.NANOSECONDS);
                        if (f == null)
                            throw new TimeoutException();
                        nanos = deadline - System.nanoTime();
                    }
                    else
                        f = ecs.take();
                }
                if (f != null) {
    
    
                    --active;
                    try {
    
    
                        return f.get();
                    } catch (ExecutionException eex) {
    
    
                        ee = eex;
                    } catch (RuntimeException rex) {
    
    
                        ee = new ExecutionException(rex);
                    }
                }
            }

            if (ee == null)
                ee = new ExecutionException();
            throw ee;

        } finally {
    
    
            for (int i = 0, size = futures.size(); i < size; i++)
                futures.get(i).cancel(true);
        }
    }

    public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
        throws InterruptedException, ExecutionException {
    
    
        try {
    
    
            return doInvokeAny(tasks, false, 0);
        } catch (TimeoutException cannotHappen) {
    
    
            assert false;
            return null;
        }
    }

    public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
                           long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException {
    
    
        return doInvokeAny(tasks, true, unit.toNanos(timeout));
    }

    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
        throws InterruptedException {
    
    
        if (tasks == null)
            throw new NullPointerException();
        ArrayList<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
        boolean done = false;
        try {
    
    
            for (Callable<T> t : tasks) {
    
    
                RunnableFuture<T> f = newTaskFor(t);
                futures.add(f);
                execute(f);
            }
            for (int i = 0, size = futures.size(); i < size; i++) {
    
    
                Future<T> f = futures.get(i);
                if (!f.isDone()) {
    
    
                    try {
    
    
                        f.get();
                    } catch (CancellationException ignore) {
    
    
                    } catch (ExecutionException ignore) {
    
    
                    }
                }
            }
            done = true;
            return futures;
        } finally {
    
    
            if (!done)
                for (int i = 0, size = futures.size(); i < size; i++)
                    futures.get(i).cancel(true);
        }
    }

    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
                                         long timeout, TimeUnit unit)
        throws InterruptedException {
    
    
        if (tasks == null)
            throw new NullPointerException();
        long nanos = unit.toNanos(timeout);
        ArrayList<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
        boolean done = false;
        try {
    
    
            for (Callable<T> t : tasks)
                futures.add(newTaskFor(t));

            final long deadline = System.nanoTime() + nanos;
            final int size = futures.size();

            // Interleave time checks and calls to execute in case
            // executor doesn't have any/much parallelism.
            for (int i = 0; i < size; i++) {
    
    
                execute((Runnable)futures.get(i));
                nanos = deadline - System.nanoTime();
                if (nanos <= 0L)
                    return futures;
            }

            for (int i = 0; i < size; i++) {
    
    
                Future<T> f = futures.get(i);
                if (!f.isDone()) {
    
    
                    if (nanos <= 0L)
                        return futures;
                    try {
    
    
                        f.get(nanos, TimeUnit.NANOSECONDS);
                    } catch (CancellationException ignore) {
    
    
                    } catch (ExecutionException ignore) {
    
    
                    } catch (TimeoutException toe) {
    
    
                        return futures;
                    }
                    nanos = deadline - System.nanoTime();
                }
            }
            done = true;
            return futures;
        } finally {
    
    
            if (!done)
                for (int i = 0, size = futures.size(); i < size; i++)
                    futures.get(i).cancel(true);
        }
    }

}

猜你喜欢

转载自blog.csdn.net/pontuss/article/details/114064693
今日推荐