為您的Java利用程序添加退失事件處理
--------------------------------------------------------------------------------
1個完全的Java利用程序,通常最少要有1個利用程序的結束點。對1般程序來講,系統開發者根據需要和個人的偏好,會
在程序結束位置,通過添加System.exit(0),或System.out(⑴),來結束程序,或不加這些指令,讓程序自然運行到結束。
如:以下典型代碼
package untitled14;
/**
* This application is to demo how an applcation end
*/
public class Test {
public Test() {
}
public static void main(String[] args) {
Test test1 = new Test();
//.................
System.out.println("hello world");
//Do something before system exit
System.exit(0);//也能夠不寫這句代碼,讓程序自然結束。
}
}
對簡單的利用系統,我們直接可以在System.exit(0)代碼履行前,添加需要在利用程序退出前需要完成的工作,如:關閉網
絡連接,關閉數據庫連接等。
但是,對比較復雜的多線程利用,線程運行的狀態較復雜,我們就很難預感程序什么時候結束,如何能在利用程序結束事件到來
時,處理我們要做的工作呢?這就用到了Java對利用程序的退出的事件出處理機制。
對當前利用程序對象的取得,Java通過Runtime靜態方法:Runtime.getRuntime()通過Runtime的 void addShutdownHook
(Thread hook) 法向Java虛擬機注冊1個shutdown鉤子事件,這樣1旦程序結束事件到來時,就運行線程hook,我們在實際應
用時候,只要將程序需要完成之前做的1些工作直接通過線程hook來完成。具體演示代碼以下:
/*****************************************************************************
本程序僅演示,如何在Java利用程序中添加系統退失事件處理機制
*****************************************************************************/
package untitled14;
import java.util.*;
import java.io.*;
/**
* This application is used to demo how to hook the event of an application
*/
public class Untitled1 {
public Untitled1() {
doShutDownWork();
}
/***************************************************************************
* This is the right work that will do before the system shutdown
* 這里為了演示,為利用程序的退出增加了1個事件處理,
* 當利用程序退出時候,將程序退出的日期寫入 d:/t.log文件
**************************************************************************/
private void doShutDownWork() {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
FileWriter fw = new FileWriter("d://t.log");
System.out.println("I'm going to end");
fw.write("the application ended! " + (new Date()).toString());
fw.close();
}
catch (IOException ex) {
}
}
});
}
/****************************************************
* 這是程序的入口,僅為演示,方法中的代碼無關緊要
***************************************************/
public static void main(String[] args) {
Untitled1 untitled11 = new Untitled1();
long s = System.currentTimeMillis();
for (int i = 0; i < 1000000000; i++) {
//在這里增加您需要處理代碼
}
long se = System.currentTimeMillis();
System.out.println(se - s);
}
}
在上述程序中,我們可以看到通過在程序中增加Runtime.getRuntime().addShutdownHook(new Thread()) 事件監聽,捕獲系統
退出消息到來,然后,履行我們所需要完成工作,從而使我們的程序更硬朗!
通常情況下,我們1般調用System.exit()方法來退出JVM,查看System.exit()的設計可以發現這個方法調用了
Runtime.getRuntime()的exit()方法,參考Runtime類結構我們可以得到關于系統退出時有關更多的方法。
exit()方法會使java JVM退出,在Jdk1.3中,如果使用addShutdownHook()方法注冊了1個線程,當通過調用exit()或通過用戶
中斷(CTRL C)被關閉后,該線程將被激活調用,可以利用這1功能來在系統退出或異常退出捕捉這1時刻,做1些必要的退出
操作。
shutdownhook(關機鉤)的主要目的是在系統中斷落后行必要的清除,例如進行網絡關閉、關閉打開的文件等操作,可以通過
addShutdownHook()方法注冊了1個這樣的關機鉤,并且允許你注冊多個關機鉤。在JVM退出之前,它會啟動所有已注冊的關機
鉤,并讓這些關機鉤線程同步履行。在1個關機鉤履行之前可使用removeShutdownHook()來刪除1個已注冊的關機鉤,也可
以調用halt()不調用關機鉤線程直接退出JVM。
下面是注冊關機鉤的例子,在addShutdownHook方法里構造了1個局部類,這個局部類實現了在系統中斷退出時要履行的1些必
要操作。在例子里,同時注冊了兩個關機鉤。
import java.lang.*;
public class TestExit{
public static void main(String[] args){
System.out.println("my java process");
//注冊1個關機鉤,當系統被退出或被異常中斷時,啟動這個關機鉤線程
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run(){
//添入你想在退出JVM之前要處理的必要操作代碼
System.out.println("T1");}
});
//注冊第2個關機鉤
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run(){ System.out.println("T2");}
});
System.exit(0);
}
}
當測試這段代碼時,系統可能輸出結果以下:
my java process
T2
T1
原來,這兩個關機鉤線程在程序退出被JVM并行履行,如果你設置了線程優先級,將先履行1個高優先級的鉤子線程,否則將被
隨機并行履行。
package Thread;
/**
* 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.");
}
}
/**
>>> shutdown hook registered
>>> Sleeping for 5 seconds, try ctrl-C now if you like.
>>> Slept for 10 seconds and the main thread exited.
>>> About to execute: Thread.ShutdownHook.run() to clean up before JVM exits.
0
1
2
3
4
5
6
>>> Finished execution: Thread.ShutdownHook.run()