日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RunTime.getRunTime().addShutdownHook的用法

發布時間:2024/2/28 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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?
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的RunTime.getRunTime().addShutdownHook的用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。