/** * 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); } }