转载自 https://www.liangzl.com/get-article-detail-8064.html
有时候我们用到的程序不一定总是在JVM里面驻守,可能调用完就不用了,释放资源.
RunTime.getRunTime().addShutdownHook的作用就是在JVM销毁前执行的一个线程.当然这个线程依然要自己写.
利用这个性质,如果我们之前定义了一系列的线程池供程序本身使用,那么就可以在这个最后执行的线程中把这些线程池优雅的关闭掉.
比如我们定义了一个线程池
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销毁前无论有没有执行的线程都会进行中断,然后关闭线程池.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
今天在阅读Tomcat源码的时候,catalina这个类中使用了下边的代码,不是很了解,所以google了一下,然后测试下方法,Tomcat中的相关代码如下:
Runtime.getRuntime().addShutdownHook(shutdownHook);
这个方法的含义说明:
这个方法的意思就是在jvm中增加一个关闭的钩子,当jvm关闭的时候,会执行系统中已经设置的所有通过方法addShutdownHook添加的钩子,当系统执行完这些钩子后,jvm才会关闭。所以这些钩子可以在jvm关闭的时候进行内存清理、对象销毁等操作。
一、编写个测试类
package com.test.hook;
public class TestShutdownHook {
/**
* @param args
*/
public static void main(String[] args) {
// 定义线程1
Thread thread1 = new Thread() {
public void run() {
System.out.println("thread1...");
}
};
// 定义线程2
Thread thread2 = new Thread() {
public void run() {
System.out.println("thread2...");
}
};
// 定义关闭线程
Thread shutdownThread = new Thread() {
public void run() {
System.out.println("shutdownThread...");
}
};
// jvm关闭的时候先执行该线程钩子
Runtime.getRuntime().addShutdownHook(shutdownThread);
thread1.start();
thread2.start();
}
}
打印结果:
thread2...
thread1...
shutdownThread...
或者:
thread2...
thread1...
shutdownThread...
结论:
无论是先打印thread1还是thread2,shutdownThread 线程都是最后执行的(因为这个线程是在jvm执行关闭前才会执行)。
---------------------
作者:O溺水的鱼0
来源:CSDN
原文:https://blog.csdn.net/wgw335363240/article/details/5854402
版权声明:本文为博主原创文章,转载请附上博文链接!