J.U.C之线程池

 先看一下线程池的基础架构图:

  1. Executor
    Executor,任务的执行者,线程池框架中几乎所有类都直接或者间接实现Executor接口,它是线程池框架的基础。Executor提供了一种将“任务提交”与“任务执行”分离开来的机制,它仅提供了一个Execute()方法用来执行已经提交的Runnable任务。 
    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.
         *
         * @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);
    }
  2. ExecutorService
    ExecutorService,继承Executor,它是“执行者服务”接口,它是为"执行者接口Executor"服务而存在的。准确的地说,ExecutorService提供了“将任务提交给执行者的接口(submit方法)”,“让执行者执行任务(invokeAll, invokeAny方法)”的接口等等。 
    public interface ExecutorService extends Executor {
    /**
    * 启动一次顺序关闭,执行以前提交的任务,但不接受新任务
    */
    void shutdown();
    
    /**
    * 试图停止所有正在执行的活动任务,暂停处理正在等待的任务,并返回等待执行的任务列表
    */
    List<Runnable> shutdownNow();
    
    /**
    * 如果此执行程序已关闭,则返回 true。
    */
    boolean isShutdown();
    
    /**
    * 如果关闭后所有任务都已完成,则返回 true
    */
    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;}
  3. AbstractExecutorService
    抽象类,实现ExecutorService接口,为其提供默认实现。
    AbstractExecutorService除了实现ExecutorService接口外,还提供了newTaskFor()方法返回一个RunnableFuture,在运行的时候,它将调用底层可调用任务,作为 Future 任务,它将生成可调用的结果作为其结果,并为底层任务提供取消操作。
  4. ScheduleExecutorService
    继承ExcutorService,是一个“延迟”和“定期执行”的ExecutorService,提供了一些方法安排任务按照给定的条件延时执行或者周期性执行。
    // 创建并执行在给定延迟后启用的 ScheduledFuture。
    <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
    
    // 创建并执行在给定延迟后启用的一次性操作。
    ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
    
    // 创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;
    //也就是将在 initialDelay 后开始执行,然后在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。
    ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
    
    // 创建并执行一个在给定初始延迟后首次启用的定期操作,随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟。
    ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
  5. ThreadPoolExecutor
  6. ScheduledThreadPoolExecutor
    ScheduledThreadPoolExecutor继承ThreadPoolExecutor并且实现ScheduledExecutorService接口,相当于提供了“延迟”和“周期执行”功能的ThreadPoolExecutor。
  7. Executors
    静态工厂类,提供了Executor、ExecutorService、ScheduledExecutorService、ThreadFactory 、Callable 等类的静态工厂方法,通过这些工厂方法我们可以得到相对应的对象。 
    (1)创建并返回设置有常用配置字符串的 ExecutorService 的方法。
    (2)创建并返回设置有常用配置字符串的 ScheduledExecutorService 的方法。
    (3)创建并返回“包装的”ExecutorService 方法,它通过使特定于实现的方法不可访问来禁用重新配置。
    (4)创建并返回 ThreadFactory 的方法,它可将新创建的线程设置为已知的状态。
    (5)创建并返回非闭包形式的 Callable 的方法,这样可将其用于需要 Callable 的执行方法中。

猜你喜欢

转载自www.cnblogs.com/CHMaple/p/9283678.html