为ExecutorService增加shutdown hook
public class ShutdownHook {
private static final ShutdownHook INSTANCE = new ShutdownHook();
private List<ExecutorService> executorServices = Lists.newArrayList();
private AtomicBoolean closed = new AtomicBoolean(false);
public static ShutdownHook getInstance() {
return INSTANCE;
}
private ShutdownHook() {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
shutdown();
}
});
}
@PreDestroy
public void shutdown() {
if (!closed.compareAndSet(false, true)) {
return;
}
for (ExecutorService executorService : executorServices) {
tryShutdownNow(executorService);
}
}
private void tryShutdownNow(ExecutorService executorService) {
try {
executorService.shutdownNow();
} catch (Throwable e) {
//ignore logger maybe has been destroyed
}
try {
executorService.awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
//ignore logger maybe has been destroyed
}
}
/**
* 注册executorService
*
* @param executorService
*/
public ExecutorService register(ExecutorService executorService) {
this.executorServices.add(executorService);
return executorService;
}
}
为ExecutorService增加shutdown hook的更多相关文章
- Java Shutdown Hook 场景使用和源码分析
我是陈皮,一个在互联网 Coding 的 ITer,微信搜索「陈皮的JavaLib」第一时间阅读最新文章,回复[资料],即可获得我精心整理的技术资料,电子书籍,一线大厂面试资料和优秀简历模板. 背景 ...
- 关闭钩子(shutdown hook)的作用
DK1.3介绍了java.lang.Runtime class的addShutdownHook()方法.如果你需要在你的程序关闭前采取什么措施,那么关闭钩子(shutdown hook)是很有用的. ...
- Java关闭钩子的应用 - Shutdown Hook
背景 在开发中,遇到这种情况,多个线程同时工作,突然一个线程遇到了fetal的错误,需要立即终止程序,等人工排查解决了问题之后重新启动.但是这样会有一个问题,程序终止时,其他线程可能正在进行重要操作, ...
- java的关闭钩子(Shutdown Hook)
Runtime.getRuntime().addShutdownHook(shutdownHook); 这个方法的含义说明: 这个方法的意思就是在jvm中增加一个关闭的钩子,当jv ...
- 关闭钩子(shutdown hook)的作用以及在Tomcat中的使用
在很多实际应用环境中,当用户关了应用程序时,需要做一些善后清理工作,但问题是,用户有时并不会按照推荐的方法关闭应用程序,很有可能不做清理工作,例如在Tomcat的部署应用中,通过实例化一个Server ...
- JAVA虚拟机关闭钩子(Shutdown Hook)
程序经常也会遇到进程挂掉的情况,一些状态没有正确的保存下来,这时候就需要在JVM关掉的时候执行一些清理现场的代码.JAVA中的ShutdownHook提供了比较好的方案. JDK提供了Java.Run ...
- ExecutorService对象的shutdown()和shutdownNow()的区别
可以关闭 ExecutorService,这将导致其拒绝新任务.提供两个方法来关闭 ExecutorService. shutdown() 方法在终止前允许执行以前提交的任务; shutdownNow ...
- java中Executor、ExecutorService、ThreadPoolExecutor介绍(转)
1.Excutor 源码非常简单,只有一个execute(Runnable command)回调接口 public interface Executor { /** * Executes th ...
- java并发编程框架 Executor ExecutorService invokeall
首先介绍两个重要的接口,Executor和ExecutorService,定义如下: public interface Executor { void execute(Runnable command ...
随机推荐
- Raspberry Camera详解+picamera库+Opencv控制
使用树莓派的摄像头,将树莓派自身提供的picamera的API数据转换为Python Oencv可用图像数据: # import the necessary packages from picamer ...
- C语言中的模运算-hdu6124(打表,找规律)
题目链接:https://vjudge.net/problem/HDU-6124 题目描述: 题目大意就是给你一个数,判断这个数 % 其它数后共有几种结果. 这题对我来说最大的难点是我不太知道每个数 ...
- TF之RNN:实现利用scope.reuse_variables()告诉TF想重复利用RNN的参数的案例—Jason niu
import tensorflow as tf # 22 scope (name_scope/variable_scope) from __future__ import print_function ...
- Full Tank? POJ - 3635 (bfs | 最短路)
After going through the receipts from your car trip through Europe this summer, you realised that th ...
- ssh centos 上传文件
ssh centos 上传文件命令(ftp开不起的情报况下): rz -be 下载文件: sz
- Linux 默认目录
/etc 存放系统管理所需要的配置文件和子目录 /home 一般用户的主目录 /usr 用户使用的系统目录和应用程序等信息 /bin 存放使用者经常使用的命令 如cp ls cat 等 /proc ...
- python urllib 库
urllib模块中的方法 1.urllib.urlopen(url[,data[,proxies]]) 打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作.本例试着打开google ...
- BZOJ.5287.[AHOI HNOI2018]毒瘤(虚树 树形DP)
BZOJ LOJ 洛谷 设\(f[i][0/1]\)表示到第\(i\)个点,不选/选这个点的方案数.对于一棵树,有:\[f[x][0]=\prod_{v\in son[x]}(f[v][0]+f[v] ...
- Shell脚本笔记(一)一些零碎的基础知识
一些零碎的基础知识 一.认识Shell脚本 一)相关概念 Shell是一种命令解释器,作用是按次序执行(遇到子脚本,先执行子脚本的命令)用户输入的命令和程序. Shell脚本语言是弱类型语言,与其他脚 ...
- windows安装channels报错的解决方案
windows作为开发机真是让人又爱又恨,总是会遇到各种各样的问题 报错 以安装channels为例:如: pip install channels 时出现: error: Microsoft V ...