如何使用Runtime.addShutdownHook
2007-03-08 16:37
405 查看
根据 Java API, 所谓 shutdown hook 就是已经初始化但尚未开始执行的线程对象。在
Runtime 注册后,如果 jvm 要停止前,这些 shutdown hook 便开始执行。
有什么用呢?就是在你的程序结束前,执行一些清理工作,尤其是没有用户界面的程序。
很明显,这些 shutdown hook 都是些线程对象,因此,你的清理工作要写在 run() 里。
根据 Java API,你的清理工作不能太重了,要尽快结束。但仍然可以对数据库进行操作。
举例如下:
package john2;
/**
* 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.");
}
}
参考资料:
1. Java API Documentation
2. http://java.sun.com/j2se/1.3/docs/guide/lang/hook-design.html
Runtime 注册后,如果 jvm 要停止前,这些 shutdown hook 便开始执行。
有什么用呢?就是在你的程序结束前,执行一些清理工作,尤其是没有用户界面的程序。
很明显,这些 shutdown hook 都是些线程对象,因此,你的清理工作要写在 run() 里。
根据 Java API,你的清理工作不能太重了,要尽快结束。但仍然可以对数据库进行操作。
举例如下:
package john2;
/**
* 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.");
}
}
参考资料:
1. Java API Documentation
2. http://java.sun.com/j2se/1.3/docs/guide/lang/hook-design.html
相关文章推荐
- 如何使用Runtime.addShutdownHook
- 如何使用Runtime.addShutdownHook(Thread)
- Runtime.getRuntime().addShutdownHook()的使用
- Runtime.addShutdownHook()使用
- Runtime.getRuntime().addShutdownHook()的使用
- Runtime.getRuntime().addShutdownHook()的使用
- Runtime.getRuntime().addShutdownHook(Thread hook)的使用
- Runtime.getRuntime().addShutdownHook(Thread)
- Runtime.getRuntime().addShutdownHook(new Thread())
- Runtime.addShutdownHook的用法
- Runtime.addShutdownHook用法
- 利用 java.lang.Runtime.addShutdownHook() 钩子程序,保证java程序安全退出
- runtime.getruntime().addshutdownhook
- Runtime.getRuntime().addShutdownHook(new Thread()
- RunTime.getRunTime().addShutdownHook
- JAVA Runtime.addShutdownHook()方法
- JAVA Runtime.addShutdownHook()方法{拿到线程句柄,在程序关闭之前调用释放资源}
- java(JVM)结束时释放JNI资源(Runtime.addShutdownHook)
- Runtime.getRuntime().addShutdownHook(thread)的作用
- Runtime.addShutdownHook用法