• 聊聊高并发(三十九)解析java.util.concurrent各个组件(十五) 理解ExecutorService接口的设计


    上一篇讲了Executor接口的设计,目的是将任务的运行和任务的提交解耦。能够隐藏任务的运行策略。这篇说说ExecutorService接口。它扩展了Executor接口,对Executor的生命周期进行管理。并进行了进一步的扩展。


    Executor负责执行任务。

    它的生命周期有3个:执行,关闭和已终止。

    在执行的不论什么时刻,有些 任务可能已经完毕,有些可能正在执行,有些可能正在队列中等待执行。所以假设要关闭Executor的话。就有多种方式,比方优雅平滑的关闭,当执行关闭时就不在接受新的任务请求,而且等到已执行的任务执行完毕再关闭Executor;比方最粗暴的关闭,直接关闭Executor,无论在执行的任务是否执行完毕。

    1. shutdown()方法是优雅关闭的方式

    2. shutdownNow()是粗暴关闭的方式。它返回一个还未開始运行的任务的列表。

    对于正在运行的任务,它採用Thread.interrupt()的方式来取消任务。

    假设任务不响应线程的中断,那么这个任务可能会一直运行。

    3. awaitTermination()方法会等待一段时间再来终止运行的任务

    public interface ExecutorService extends Executor {
    
        void shutdown();
    
        List<Runnable> shutdownNow();
        
        boolean isShutdown();
    
        
        boolean isTerminated();
    
        boolean awaitTermination(long timeout, TimeUnit unit)
            throws InterruptedException;
    
    
        <T> Future<T> submit(Callable<T> task);
    
        
        <T> Future<T> submit(Runnable task, T result);
    
       
        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) throws InterruptedException, ExecutionException; <T> T invokeAny(Collection<?

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


    ExecutorService的JavaDoc里面的这段代码展示了两阶段来关闭Executor的方式

    1. 先用shutdown()来停止接受新的任务

    2. 再用awaitTermination()方法等待一段时间来让运行的任务运行完毕。再shutdownNow()强制关闭(採用Thread.interrupt()的方式)。再次awaitTermination()等待一段时间,这里是针对上面说的不响应Thread.interrupt()的任务。假设还没有terminated。就提示Executor没有完毕关闭,还有线程在运行任务。

     void shutdownAndAwaitTermination(ExecutorService pool) {
       pool.shutdown(); // Disable new tasks from being submitted
      try {
         // Wait a while for existing tasks to terminate
          if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
          pool.shutdownNow(); // Cancel currently executing tasks
            // Wait a while for tasks to respond to being cancelled
           if (!pool.awaitTermination(60, TimeUnit.SECONDS))
               System.err.println("Pool did not terminate");
          }
        } catch (InterruptedException ie) {
         // (Re-)Cancel if current thread also interrupted
          pool.shutdownNow();
          // Preserve interrupt status
         Thread.currentThread().interrupt();
        }
     }

    除了关闭Executor和检查Executor状态的方法外,ExecutorService还定义了一系列的submit()和invoke()来支持任务异步运行和批量运行。

    submit()方法支持Callable接口和普通的Runnable接口,它的目的是返回一个Future对象来支持任务的异步运行。调用者能够通过Future对象来检查任务的运行状态。后面会详细介绍Future相关的内容。

    invokeAll()和invokeAny()提供了批量运行任务的接口。


  • 相关阅读:
    维基百科可以浏览了,不能搜索
    《语言的本能》很值得一读
    打算尝试瑜伽,看是否能解决长期困扰的胃痛问题
    折腾了一晚终于把rails安装好了!
    世界杯正在成为鸡肋!球迷越来越像傻瓜!
    第一次愉快的通过豆瓣购书
    我对少林方丈释永信的一点初浅看法
    myet: 练习英语口语不错
    蚂蚁社区和胃病治疗诀窍
    装好了ubuntu,w2k却无法启动了,:(
  • 原文地址:https://www.cnblogs.com/lytwajue/p/6814871.html
Copyright © 2020-2023  润新知