為您的Java利用程序添加退失事件處理
--------------------------------------------------------------------------------
1個完全的Java利用程序,通常最少要有1個利用程序的結(jié)束點(diǎn)。對1般程序來講,系統(tǒng)開發(fā)者根據(jù)需要和個人的偏好,會
在程序結(jié)束位置,通過添加System.exit(0),或System.out(⑴),來結(jié)束程序,或不加這些指令,讓程序自然運(yùn)行到結(jié)束。
如:以下典型代碼
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);//也能夠不寫這句代碼,讓程序自然結(jié)束。
}
}
對簡單的利用系統(tǒng),我們直接可以在System.exit(0)代碼履行前,添加需要在利用程序退出前需要完成的工作,如:關(guān)閉網(wǎng)
絡(luò)連接,關(guān)閉數(shù)據(jù)庫連接等。
但是,對比較復(fù)雜的多線程利用,線程運(yùn)行的狀態(tài)較復(fù)雜,我們就很難預(yù)感程序什么時候結(jié)束,如何能在利用程序結(jié)束事件到來
時,處理我們要做的工作呢?這就用到了Java對利用程序的退出的事件出處理機(jī)制。
對當(dāng)前利用程序?qū)ο蟮娜〉茫琂ava通過Runtime靜態(tài)方法:Runtime.getRuntime()通過Runtime的 void addShutdownHook
(Thread hook) 法向Java虛擬機(jī)注冊1個shutdown鉤子事件,這樣1旦程序結(jié)束事件到來時,就運(yùn)行線程hook,我們在實(shí)際應(yīng)
用時候,只要將程序需要完成之前做的1些工作直接通過線程hook來完成。具體演示代碼以下:
/*****************************************************************************
本程序僅演示,如何在Java利用程序中添加系統(tǒng)退失事件處理機(jī)制
*****************************************************************************/
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āng)利用程序退出時候,將程序退出的日期寫入 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) {
}
}
});
}
/****************************************************
* 這是程序的入口,僅為演示,方法中的代碼無關(guān)緊要
***************************************************/
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()) 事件監(jiān)聽,捕獲系統(tǒng)
退出消息到來,然后,履行我們所需要完成工作,從而使我們的程序更硬朗!
通常情況下,我們1般調(diào)用System.exit()方法來退出JVM,查看System.exit()的設(shè)計可以發(fā)現(xiàn)這個方法調(diào)用了
Runtime.getRuntime()的exit()方法,參考Runtime類結(jié)構(gòu)我們可以得到關(guān)于系統(tǒng)退出時有關(guān)更多的方法。
exit()方法會使java JVM退出,在Jdk1.3中,如果使用addShutdownHook()方法注冊了1個線程,當(dāng)通過調(diào)用exit()或通過用戶
中斷(CTRL C)被關(guān)閉后,該線程將被激活調(diào)用,可以利用這1功能來在系統(tǒng)退出或異常退出捕捉這1時刻,做1些必要的退出
操作。
shutdownhook(關(guān)機(jī)鉤)的主要目的是在系統(tǒng)中斷落后行必要的清除,例如進(jìn)行網(wǎng)絡(luò)關(guān)閉、關(guān)閉打開的文件等操作,可以通過
addShutdownHook()方法注冊了1個這樣的關(guān)機(jī)鉤,并且允許你注冊多個關(guān)機(jī)鉤。在JVM退出之前,它會啟動所有已注冊的關(guān)機(jī)
鉤,并讓這些關(guān)機(jī)鉤線程同步履行。在1個關(guān)機(jī)鉤履行之前可使用removeShutdownHook()來刪除1個已注冊的關(guān)機(jī)鉤,也可
以調(diào)用halt()不調(diào)用關(guān)機(jī)鉤線程直接退出JVM。
下面是注冊關(guān)機(jī)鉤的例子,在addShutdownHook方法里構(gòu)造了1個局部類,這個局部類實(shí)現(xiàn)了在系統(tǒng)中斷退出時要履行的1些必
要操作。在例子里,同時注冊了兩個關(guān)機(jī)鉤。
import java.lang.*;
public class TestExit{
public static void main(String[] args){
System.out.println("my java process");
//注冊1個關(guān)機(jī)鉤,當(dāng)系統(tǒng)被退出或被異常中斷時,啟動這個關(guān)機(jī)鉤線程
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run(){
//添入你想在退出JVM之前要處理的必要操作代碼
System.out.println("T1");}
});
//注冊第2個關(guān)機(jī)鉤
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run(){ System.out.println("T2");}
});
System.exit(0);
}
}
當(dāng)測試這段代碼時,系統(tǒng)可能輸出結(jié)果以下:
my java process
T2
T1
原來,這兩個關(guān)機(jī)鉤線程在程序退出被JVM并行履行,如果你設(shè)置了線程優(yōu)先級,將先履行1個高優(yōu)先級的鉤子線程,否則將被
隨機(jī)并行履行。
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()