• Executor


    1、Executor是任务执行器,该接口里面只含有一个方法。原来创建新的线程都是采用new Thread(new Runnable())的方式创建,该接口将原来提交任务的方式进行了进一步的抽象。

     /**

         * 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 <tt>Executor</tt> 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   该接口继承自Executor,增加了线程管理器的生命周期的方法,比如shutdown,判断是否已shutdown,submit等方法。定义了完整的线程池的方法。

                        关闭线程池的两种方法:1shutdown()   会拒绝新的任务,但是会等待已提交的线程全部完成

                                                      2shutdownnow()会拒绝新的任务,同时结束正在运行中的任务。立刻结束

                                                      注意:因为有的线程可能捕获不到interruptedException,这导致可能有的线程根本不能被停止。

                        如何优雅的关闭线程池:

     

  • 相关阅读:
    基于Tire树和最大概率法的中文分词功能的Java实现
    通过 Spring RestTemplate 调用带请求体的 Delete 方法(Delete With Request Body)
    《高效能程序员的修炼》之译者序(本书将于7月初上市,人民邮电出版社)
    JVM类加载过程学习总结
    Web安全之Cookie劫持
    2016第40周五
    年轻By塞缪尔·厄尔曼
    2016第40周三
    2016第40周二
    果断歌
  • 原文地址:https://www.cnblogs.com/YDDMAX/p/5204183.html
Copyright © 2020-2023  润新知