RunTime.getRunTime().addShutdownHook的用法
轉載自?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?
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
總結
以上是生活随笔為你收集整理的RunTime.getRunTime().addShutdownHook的用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java retry(重试) sprin
- 下一篇: RecursiveTask和Recurs