zoukankan      html  css  js  c++  java
  • 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,这导致可能有的线程根本不能被停止。

                        如何优雅的关闭线程池:

     

  • 相关阅读:
    HDU 5273 Dylans loves sequence 暴力递推
    HDU 5285 wyh2000 and pupil 判二分图+贪心
    HDU 5281 Senior's Gun 贪心
    HDU 5651 xiaoxin juju needs help 逆元
    HDU 5646 DZY Loves Partition
    HDU 5366 The mook jong
    HDU 5391Z ball in Tina Town 数论
    HDU 5418 Victor and World 允许多次经过的TSP
    HDU 5642 King's Order dp
    抽屉原理
  • 原文地址:https://www.cnblogs.com/YDDMAX/p/5204183.html
Copyright © 2011-2022 走看看