[pre]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);//也能不写这句代码,让程式自然结束。 }}[/pre]
[pre]/*****************************************************************************本程式仅演示,怎么在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 * 这里为了演示,为应用程式的退出增加了一个事件处理, * 当应用程式退出时候,将程式退出的日期写入 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("im 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); }}[/pre]