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,这导致可能有的线程根本不能被停止。

                        如何优雅的关闭线程池:

     

  • 相关阅读:
    leetcode122
    leetcode121
    leetcode773
    leetcode803
    leetcode658
    leetcode723
    leetcode134
    leetcode340
    leetcode721
    leetcode362
  • 原文地址:https://www.cnblogs.com/YDDMAX/p/5204183.html
Copyright © 2011-2022 走看看