博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Executor
阅读量:4599 次
发布时间:2019-06-09

本文共 822 字,大约阅读时间需要 2 分钟。

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

 

                    如何优雅的关闭线程池:

 

 

 

 

转载于:https://www.cnblogs.com/YDDMAX/p/5204183.html

你可能感兴趣的文章
esp32-智能语音-cli(调试交互命令)
查看>>
netty与MQ使用心得
查看>>
关于dl dt dd 文字过长换行在移动端显示对齐的探讨总结
查看>>
swoolefy PHP的异步、并行、高性能网络通信引擎内置了Http/WebSocket服务器端/客户端...
查看>>
Python学习笔记
查看>>
unshift()与shift()
查看>>
使用 NPOI 、aspose实现execl模板公式计算
查看>>
行为型模式:中介者模式
查看>>
How to Notify Command to evaluate in mvvmlight
查看>>
33. Search in Rotated Sorted Array
查看>>
461. Hamming Distance
查看>>
Python垃圾回收机制详解
查看>>
{面试题1: 赋值运算符函数}
查看>>
Node中没搞明白require和import,你会被坑的很惨
查看>>
Python 标识符
查看>>
Python mysql 创建连接
查看>>
企业化的性能测试简述---如何设计性能测试方案
查看>>
centos7 安装中文编码
查看>>
POJ - 3683 Priest John's Busiest Day
查看>>
正则表达式start(),end(),group()方法
查看>>