有时候我们用到的程序不一定总是在JVM里面驻守,可能调用完就不用了,释放资源.

RunTime.getRunTime().addShutdownHook的作用就是在JVM销毁前执行的一个线程.当然这个线程依然要自己写.

利用这个性质,如果我们之前定义了一系列的线程池供程序本身使用,那么就可以在这个最后执行的线程中把这些线程池优雅的关闭掉.

github地址点击此处

比如我们定义了一个线程池

private ExecutorService streamThreadPool = Executors.newFixedThreadPool(streamNum);

然后我们需要对它进行优雅关闭

Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
shutdownGracefully();
}
});
public void shutdownGracefully() {
shutdownThreadPool(streamThreadPool, "main-pool");
} /**
* 优雅关闭线程池
* @param threadPool
* @param alias
*/
private void shutdownThreadPool(ExecutorService threadPool, String alias) {
log.info("Start to shutdown the thead pool: {}", alias); threadPool.shutdown(); // 使新任务无法提交.
try {
// 等待未完成任务结束
if (!threadPool.awaitTermination(60, TimeUnit.SECONDS)) {
threadPool.shutdownNow(); // 取消当前执行的任务
log.warn("Interrupt the worker, which may cause some task inconsistent. Please check the biz logs."); // 等待任务取消的响应
if (!threadPool.awaitTermination(60, TimeUnit.SECONDS))
log.error("Thread pool can't be shutdown even with interrupting worker threads, which may cause some task inconsistent. Please check the biz logs.");
}
} catch (InterruptedException ie) {
// 重新取消当前线程进行中断
threadPool.shutdownNow();
log.error("The current server thread is interrupted when it is trying to stop the worker threads. This may leave an inconcistent state. Please check the biz logs."); // 保留中断状态
Thread.currentThread().interrupt();
} log.info("Finally shutdown the thead pool: {}", alias);
}

这样我们就可以在JVM销毁前无论有没有执行的线程都会进行中断,然后关闭线程池.

使用RunTime.getRunTime().addShutdownHook优雅关闭线程池的更多相关文章

  1. ThreadPoolExecutor 优雅关闭线程池的原理.md

    经典关闭线程池代码 ExecutorService executorService = Executors.newFixedThreadPool(10); executorService.shutdo ...

  2. Runtime.getRuntime().addShutdownHook(shutdownHook);

    今天在阅读Tomcat源码的时候,catalina这个类中使用了下边的代码,不是很了解,所以google了一下,然后测试下方法,Tomcat中的相关代码如下: Runtime.getRuntime() ...

  3. 关于jvm钩子 Runtime.getRuntime().addShutdownHook

    转自: http://www.cnblogs.com/nexiyi/p/java_add_ShutdownHook.html 在线上Java程序中经常遇到进程程挂掉,一些状态没有正确的保存下来,这时候 ...

  4. RunTime.getRunTime().addShutdownHook用法

    今天在阅读Tomcat源码的时候,catalina这个类中使用了下边的代码,不是很了解,所以google了一下,然后测试下方法,Tomcat中的相关代码如下: Runtime.getRuntime() ...

  5. Runtime.getRuntime().addShutdownHook(Thread)

    Runtime.getRuntime().addShutdownHook(Thread)为虚拟机添加关闭时添加钩子线程

  6. Java中RunTime.getRunTime().addShutdownHook用法

    今天在阅读Tomcat源码的时候,catalina这个类中使用了下边的代码,不是很了解,所以google了一下,然后测试下方法,Tomcat中的相关代码如下: Runtime.getRuntime() ...

  7. RunTime.getRunTime().addShutdownHook 添加钩子

    Runtime.getRuntime().addShutdownHook(shutdownHook); google了一下它的含义:在jvm中增加一个关闭的钩子,当jvm关闭的时候,会执行系统中已经设 ...

  8. Runtime.getRuntime().addShutdownHook

      Runtime.getRuntime().addShutdownHook(shutdownHook); 这个方法的含义说明: 这个方法的意思就是在jvm中增加一个关闭的钩子,当jvm关闭的时候,会 ...

  9. 【转】RunTime.getRunTime().addShutdownHook用法

    Runtime.getRuntime().addShutdownHook(shutdownHook); 这个方法的含义说明: 这个方法的意思就是在jvm中增加一个关闭的钩子,当jvm关闭的时候,会执行 ...

随机推荐

  1. HeadFirst设计模式---抽象工厂

    类图 抽象披萨商店类 public abstract class PizzaStore { public void orderPizza(String type) { AbstractPizza ab ...

  2. Win2003下配置iis+php+mysql+zend

    所需软件: ActivePerl.PHP.MYSQL.Zend (一.安装IIS6.0;二.配置PHP环境;三.安装mysql;四.安装 Zend Optimizer;五.配置PHPMYADMIN) ...

  3. CRT&EXCRT学习笔记

    非扩展 用于求解线性同余方程组 ,其中模数两两互质 . 先来看一看两个显然的定理: 1.若 x \(\equiv\) 0 (mod p) 且 y \(\equiv\) 0 (mod p) ,则有 x+ ...

  4. centos 下 gradle 编译打包 apk

    由于Jenkins 装在centos环境下,想实现Android程序的编译,只能通过gradle 命令去打包版本apk,以下记录了如何在centos下使用gradle 打包apk 一.安装 gradl ...

  5. WARN Connection to node 2 could not be established. Broker may not be available. (

    启动 kafka 集群, 出现这个问题 WARN Connection to node 2 could not be established. Broker may not be available. ...

  6. .Net反射-两种方式获取Enum中的值

    public enum EJobType { 客服 = , 业务员 = , 财务 = , 经理 = } Type jobType = typeof(EJobType); 方式1: Array enum ...

  7. mariadb指定10.2版本安装及修改默认端口

    原文链接: https://www.cnblogs.com/operationhome/p/9141881.html 延申, mongodb, mariadb:  https://www.cnblog ...

  8. 洛谷p3385【模板】负环

    最近很久没怎么写最短路的题导致这个题交了好多遍 AC率是怎么下来的自己心里没点数 SPFA虽然臭名昭著但是他可以用来判负环 如果一个点进队的次数大于等于n说明存在负环 这道题一开始memset我给di ...

  9. [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  10. Windows的一些使用技巧/设置

    仅为个人记录,关闭与否还请读者斟酌 1,加速关机速度 运行gpedit.msc: 计算机管理,管理模块 - 系统 -关机选项 关闭会阻止或取消关机的应用程序的自动终止功能. 2,组策略关闭小娜后,只把 ...