/** * 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);//也可以不寫這句代碼,讓程序自然結束。 } }
/** * 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); } }