RunTime.getRunTime().addShutdownHook的用法
轉(zhuǎn)載自?https://www.liangzl.com/get-article-detail-8064.html
有時(shí)候我們用到的程序不一定總是在JVM里面駐守,可能調(diào)用完就不用了,釋放資源.
RunTime.getRunTime().addShutdownHook的作用就是在JVM銷毀前執(zhí)行的一個(gè)線程.當(dāng)然這個(gè)線程依然要自己寫.
利用這個(gè)性質(zhì),如果我們之前定義了一系列的線程池供程序本身使用,那么就可以在這個(gè)最后執(zhí)行的線程中把這些線程池優(yōu)雅的關(guān)閉掉.
比如我們定義了一個(gè)線程池
private ExecutorService streamThreadPool = Executors.newFixedThreadPool(streamNum);然后我們需要對它進(jìn)行優(yōu)雅關(guān)閉
Runtime.getRuntime().addShutdownHook(new Thread() {public void run() {shutdownGracefully();} }); public void shutdownGracefully() {shutdownThreadPool(streamThreadPool, "main-pool"); }/*** 優(yōu)雅關(guān)閉線程池* @param threadPool* @param alias*/ private void shutdownThreadPool(ExecutorService threadPool, String alias) {log.info("Start to shutdown the thead pool: {}", alias);threadPool.shutdown(); // 使新任務(wù)無法提交.try {// 等待未完成任務(wù)結(jié)束if (!threadPool.awaitTermination(60, TimeUnit.SECONDS)) {threadPool.shutdownNow(); // 取消當(dāng)前執(zhí)行的任務(wù)log.warn("Interrupt the worker, which may cause some task inconsistent. Please check the biz logs.");// 等待任務(wù)取消的響應(yīng)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) {// 重新取消當(dāng)前線程進(jìn)行中斷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.");// 保留中斷狀態(tài)Thread.currentThread().interrupt();}log.info("Finally shutdown the thead pool: {}", alias); }這樣我們就可以在JVM銷毀前無論有沒有執(zhí)行的線程都會(huì)進(jìn)行中斷,然后關(guān)閉線程池.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
今天在閱讀Tomcat源碼的時(shí)候,catalina這個(gè)類中使用了下邊的代碼,不是很了解,所以google了一下,然后測試下方法,Tomcat中的相關(guān)代碼如下:
??????Runtime.getRuntime().addShutdownHook(shutdownHook);
???這個(gè)方法的含義說明:
???????這個(gè)方法的意思就是在jvm中增加一個(gè)關(guān)閉的鉤子,當(dāng)jvm關(guān)閉的時(shí)候,會(huì)執(zhí)行系統(tǒng)中已經(jīng)設(shè)置的所有通過方法addShutdownHook添加的鉤子,當(dāng)系統(tǒng)執(zhí)行完這些鉤子后,jvm才會(huì)關(guān)閉。所以這些鉤子可以在jvm關(guān)閉的時(shí)候進(jìn)行內(nèi)存清理、對象銷毀等操作。
?
一、編寫個(gè)測試類
??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...");
???}
??};
??// 定義關(guān)閉線程
??Thread shutdownThread = new Thread() {
???public void run() {
????System.out.println("shutdownThread...");
???}
??};
??// jvm關(guān)閉的時(shí)候先執(zhí)行該線程鉤子
??Runtime.getRuntime().addShutdownHook(shutdownThread);
??thread1.start();
??thread2.start();
?}
}
打印結(jié)果:
thread2...
thread1...
shutdownThread...
或者:
thread2...
thread1...
shutdownThread...
結(jié)論:
?
無論是先打印thread1還是thread2,shutdownThread 線程都是最后執(zhí)行的(因?yàn)檫@個(gè)線程是在jvm執(zhí)行關(guān)閉前才會(huì)執(zhí)行)。
---------------------?
作者:O溺水的魚0?
來源:CSDN?
原文:https://blog.csdn.net/wgw335363240/article/details/5854402?
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接!
總結(jié)
以上是生活随笔為你收集整理的RunTime.getRunTime().addShutdownHook的用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java retry(重试) sprin
- 下一篇: RecursiveTask和Recurs