ExecutorService的几种关闭线程池方法
来源:互联网 发布:os x 下载软件 编辑:程序博客网 时间:2024/06/06 20:13
ExecutorService的几种关闭线程池方法:
ExecutorService executorService =Executors.newFixedThreadPool(1);
1、shutdown()方法在终止前允许执行以前提交的任务。 这个方法会顺次地关闭ExecutorService,当我们调用这个方法时,ExecutorService停止接受任何新的任务且等待已经提交的任务执行完成(已经提交的任务会分两类:一类是已经在执行的,另一类是还没有开始执行的),当所有已经提交的任务执行完毕后将会关闭ExecutorService。
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.
This method does not wait for previously submitted tasks to complete execution. UseawaitTermination
to do that.
2、shutdownNow()方法则是阻止正在任务队列中等待任务的启动并试图停止当前正在执行的任务,返回要停止的任务List。
List<Runnable> shutdownNow();
Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.
This method does not wait for actively executing tasks to terminate. Use awaitTermination
to do that.
There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel viaThread.interrupt
, so any task that fails to respond to interrupts may never terminate.
- Returns:
- list of tasks that never commenced execution
3、awaitTermination方法:这个方法有两个参数,一个是timeout即超时时间,另一个是unit即时间单位。这个方法会使线程等待timeout时长,当超过timeout时间后,会监测ExecutorService是否已经关闭,若关闭则返回true,否则返回false。一般情况下会和shutdown方法组合使用。
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.
- Parameters:
- timeout the maximum time to wait
- unit the time unit of the timeout argument
- Returns:
true
if this executor terminated andfalse
if the timeout elapsed before termination- Throws:
- InterruptedException - if interrupted while waiting
- ExecutorService的几种关闭线程池方法
- 线程池 多线程运行结束后 如何关闭? ExecutorService的正确关闭方法
- ExecutorService的正确关闭方法
- ExecutorService的正确关闭方法
- ExecutorService创建线程池的四种方法
- 获取线程池的方法 ExecutorService
- [翻译][Java]ExecutorService的正确关闭方法
- [翻译][Java]ExecutorService的正确关闭方法
- Android的ExecutorService线程池
- ExecutorService线程池的用法
- ExecutorService线程池的用法
- ExecutorService线程池的使用
- ExecutorService线程池的用法
- ExecutorService线程池的用法
- ExecutorService线程池的使用
- ExecutorService线程池的用法
- ExecutorService线程池的用法
- 线程池 ExecutorService的使用
- Android开源项目分类汇总(六)工具库
- Linux修改内核使得普通用户可以打印kmsg内容
- 网络数据传输的封装
- 【poj 3292】Semi-prime H-numbers
- Flask、uWSGI and Nginx on Ubuntu 14.04
- ExecutorService的几种关闭线程池方法
- 貞觀 宋哲和 先生 墓所探訪
- <css 四>a href
- (4.6.9)Android属性allowBackup安全风险浅析
- Android Studio使用总结 [未完待续]
- linux的挂载磁盘步骤
- 【有毒的设计模式】代理模式
- android6.0 statusbar 通知栏颜色
- 单例模式详解