解析Runtime中shutdown hook的使用詳解
更新時(shí)間:2013年05月17日 09:47:27 作者:
本篇文章是對(duì)解析Runtime中shutdown hook的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
根據(jù) Java API, 所謂 shutdown hook 就是已經(jīng)初始化但尚未開始執(zhí)行的線程對(duì)象。在Runtime 注冊(cè)后,如果 jvm 要停止前,這些 shutdown hook 便開始執(zhí)行。聲明:Runtime.addShutdownHook(Thread t)
舉例如下:
package john2;
/**
* test shutdown hook
* All rights released and correctness not guaranteed.
*/
public class ShutdownHook implements Runnable {
public ShutdownHook() {
// register a shutdown hook for this class.
// a shutdown hook is an initialzed but not started thread, which will get up and run
// when the JVM is about to exit. this is used for short clean up tasks.
Runtime.getRuntime().addShutdownHook(new Thread(this));
System.out.println(">>> shutdown hook registered");
}
// this method will be executed of course, since it's a Runnable.
// tasks should not be light and short, accessing database is alright though.
public void run() {
System.out.println("/n>>> About to execute: " + ShutdownHook.class.getName() + ".run() to clean up before JVM exits.");
this.cleanUp();
System.out.println(">>> Finished execution: " + ShutdownHook.class.getName() + ".run()");
}
// (-: a very simple task to execute
private void cleanUp() {
for(int i=0; i < 7; i++) {
System.out.println(i);
}
}
/**
* there're couple of cases that JVM will exit, according to the Java api doc.
* typically:
* 1. method called: System.exit(int)
* 2. ctrl-C pressed on the console.
* 3. the last non-daemon thread exits.
* 4. user logoff or system shutdown.
* @param args
*/
public static void main(String[] args) {
new ShutdownHook();
System.out.println(">>> Sleeping for 5 seconds, try ctrl-C now if you like.");
try {
Thread.sleep(5000); // (-: give u the time to try ctrl-C
} catch (InterruptedException ie) {
ie.printStackTrace();
}
System.out.println(">>> Slept for 10 seconds and the main thread exited.");
}
}
也許有人會(huì)擔(dān)心性能問題,shutdown hook會(huì)不會(huì)占用太多的VM的資源,答案是shutdown hook不會(huì)占用VM太多的資源,因?yàn)閟hutdown hook 只是一個(gè)已初始化但尚未啟動(dòng)的線程。這意味著它只在程序關(guān)閉的時(shí)候才會(huì)啟動(dòng),而不是在程序一開始運(yùn)行時(shí)就啟動(dòng)。而在大多數(shù)的Java平臺(tái)中,如果一個(gè)線程沒有啟動(dòng)(即沒有調(diào)用線程的start()函數(shù))VM不會(huì)分配資源給線程。因此維護(hù)一群沒有啟動(dòng)的線程不會(huì)給VM帶來太大的負(fù)擔(dān).
最后還要注意以下兩點(diǎn):
如果VM crash,那么不能保證關(guān)閉掛鉤(shutdown hooks)能運(yùn)行.試想一下如果Windows XP突然藍(lán)屏了那么本來計(jì)劃在關(guān)機(jī)之前的更新也就無法進(jìn)行了.
如果調(diào)用Runtime.halt()方法來結(jié)束程序的話,那么關(guān)閉掛鉤(shutdown hooks)也不會(huì)執(zhí)行
舉例如下:
復(fù)制代碼 代碼如下:
package john2;
/**
* test shutdown hook
* All rights released and correctness not guaranteed.
*/
public class ShutdownHook implements Runnable {
public ShutdownHook() {
// register a shutdown hook for this class.
// a shutdown hook is an initialzed but not started thread, which will get up and run
// when the JVM is about to exit. this is used for short clean up tasks.
Runtime.getRuntime().addShutdownHook(new Thread(this));
System.out.println(">>> shutdown hook registered");
}
// this method will be executed of course, since it's a Runnable.
// tasks should not be light and short, accessing database is alright though.
public void run() {
System.out.println("/n>>> About to execute: " + ShutdownHook.class.getName() + ".run() to clean up before JVM exits.");
this.cleanUp();
System.out.println(">>> Finished execution: " + ShutdownHook.class.getName() + ".run()");
}
// (-: a very simple task to execute
private void cleanUp() {
for(int i=0; i < 7; i++) {
System.out.println(i);
}
}
/**
* there're couple of cases that JVM will exit, according to the Java api doc.
* typically:
* 1. method called: System.exit(int)
* 2. ctrl-C pressed on the console.
* 3. the last non-daemon thread exits.
* 4. user logoff or system shutdown.
* @param args
*/
public static void main(String[] args) {
new ShutdownHook();
System.out.println(">>> Sleeping for 5 seconds, try ctrl-C now if you like.");
try {
Thread.sleep(5000); // (-: give u the time to try ctrl-C
} catch (InterruptedException ie) {
ie.printStackTrace();
}
System.out.println(">>> Slept for 10 seconds and the main thread exited.");
}
}
也許有人會(huì)擔(dān)心性能問題,shutdown hook會(huì)不會(huì)占用太多的VM的資源,答案是shutdown hook不會(huì)占用VM太多的資源,因?yàn)閟hutdown hook 只是一個(gè)已初始化但尚未啟動(dòng)的線程。這意味著它只在程序關(guān)閉的時(shí)候才會(huì)啟動(dòng),而不是在程序一開始運(yùn)行時(shí)就啟動(dòng)。而在大多數(shù)的Java平臺(tái)中,如果一個(gè)線程沒有啟動(dòng)(即沒有調(diào)用線程的start()函數(shù))VM不會(huì)分配資源給線程。因此維護(hù)一群沒有啟動(dòng)的線程不會(huì)給VM帶來太大的負(fù)擔(dān).
最后還要注意以下兩點(diǎn):
如果VM crash,那么不能保證關(guān)閉掛鉤(shutdown hooks)能運(yùn)行.試想一下如果Windows XP突然藍(lán)屏了那么本來計(jì)劃在關(guān)機(jī)之前的更新也就無法進(jìn)行了.
如果調(diào)用Runtime.halt()方法來結(jié)束程序的話,那么關(guān)閉掛鉤(shutdown hooks)也不會(huì)執(zhí)行
相關(guān)文章
Spring Boot 2 Thymeleaf服務(wù)器端表單驗(yàn)證實(shí)現(xiàn)詳解
這篇文章主要介紹了Spring Boot 2 Thymeleaf服務(wù)器端表單驗(yàn)證實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11Java多線程run方法中直接調(diào)用service業(yè)務(wù)類應(yīng)注意的問題及解決
這篇文章主要介紹了Java多線程run方法中直接調(diào)用service業(yè)務(wù)類應(yīng)注意的問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06springboot 實(shí)現(xiàn)bean手動(dòng)注入操作
這篇文章主要介紹了springboot 實(shí)現(xiàn)bean手動(dòng)注入操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01簡單注解實(shí)現(xiàn)集群同步鎖(spring+redis+注解)
本文主要介紹了簡單注解實(shí)現(xiàn)集群同步鎖的步驟與方法。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01詳解Java中String,StringBuffer和StringBuilder的使用
這篇文章主要為大家詳細(xì)介紹了Java中String,StringBuffer和StringBuilder三者的區(qū)別以及使用,文中的少了講解詳細(xì),感興趣的可以了解一下2022-07-07