网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  如何使用runtime.addshutdownhook     
  文章作者:未知  文章来源:水木森林  
  查看:69次  录入:管理员--2007-11-17  
      以前从未用过 runtime.addshutdownhook(thread), 也不知道什么是 shutdown hook.
最近刚刚接触了一点,总结一下。

根据 java api, 所谓 shutdown hook 就是已经初始化但尚未开始执行的线程对象。在
runtime 注册后,如果 jvm 要停止前,这些 shutdown hook 便开始执行。

有什么用呢?就是在你的程序结束前,执行一些清理工作,尤其是没有用户界面的程序。

很明显,这些 shutdown hook 都是些线程对象,因此,你的清理工作要写在 run() 里。
根据 java api,你的清理工作不能太重了,要尽快结束。但仍然可以对数据库进行操作。

举例如下:

  1. package john2;
  2. /**
  3.  * test shutdown hook
  4.  * all rights released and correctness not guaranteed.
  5.  */
  6. public class shutdownhook implements runnable {
  7.     
  8.     public shutdownhook() {
  9.         // register a shutdown hook for this class.
  10.         // a shutdown hook is an initialzed but not started thread, which will get up and run
  11.         // when the jvm is about to exit. this is used for short clean up tasks.
  12.         runtime.getruntime().addshutdownhook(new thread(this));
  13.         system.out.println(">>> shutdown hook registered");
  14.     }
  15.     
  16.     // this method will be executed of course, since it's a runnable.
  17.     // tasks should not be light and short, accessing database is alright though.
  18.     public void run() {
  19.         system.out.println("/n>>> about to execute: " + shutdownhook.class.getname() + ".run() to clean up before jvm exits.");
  20.         this.cleanup();
  21.         system.out.println(">>> finished execution: " + shutdownhook.class.getname() + ".run()");
  22.     }
  23.     
  24.         // (-: a very simple task to execute
  25.     private void cleanup() {
  26.         for(int i=0; i < 7; i++) {
  27.             system.out.println(i);
  28.         }
  29.     }
  30.     /**
  31.      * there're couple of cases that jvm will exit, according to the java api doc.
  32.      * typically:
  33.      * 1. method called: system.exit(int)
  34.      * 2. ctrl-c pressed on the console.
  35.      * 3. the last non-daemon thread exits.
  36.      * 4. user logoff or system shutdown.
  37.      * @param args
  38.      */
  39.     public static void main(string[] args) {
  40.         
  41.         new shutdownhook();
  42.         
  43.         system.out.println(">>> sleeping for 5 seconds, try ctrl-c now if you like.");
  44.         
  45.         try {
  46.             thread.sleep(5000);     // (-: give u the time to try ctrl-c
  47.         } catch (interruptedexception ie) { 
  48.             ie.printstacktrace(); 
  49.         }
  50.         
  51.         system.out.println(">>> slept for 10 seconds and the main thread exited.");
  52.     }
  53. }


参考资料:
1. java api documentation
2. http://java.sun.com/j2se/1.3/docs/guide/lang/hook-design.html
 
 
上一篇: string.split()用法的一点经验    下一篇: 利用java技术编写桌面软件基础
  相关文档
测试实践:eclipse 之 junit 11-17
安全的代价 11-17
merlin 的魔力:merlin 的新 i/o 缓冲区的输入和输出 11-17
幸福的联姻:java 和 python 11-17
对hibernate配置文件中的映射元素详解 11-16
eclipse中用swt和jface开发入门 11-16
string类使用的例子(1) 11-17
于有状态和无状态会话bean的解释 11-17
汉字方法名和变量名!---- 原来java还可以这样用啊! 11-17
全面挖掘java excel api 使用方法 11-17
java 理论与实践: 平衡测试,第 2 部分 11-17
在j2ee web 应用中使用基于captcha 的授权模块 11-17
从server.xml到web.xml的个人发现! 11-17
利用uml序列图设计java应用程序详解 11-17
int、char、double与byte相互转换的程序 11-17
java class 映射及实用工具类完整源代码 11-17
servlet2.3 api小介 11-17
resin服务器平台介绍 11-17
linux下安装整合apache和tomcat全过程 11-16
blind和shutter的比较 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息