JR 精品文章 - 如何使用Runtime.addShutdownHook(Thread)?
AD: jr (at) javaresearch.org


首页 | 动态 | 文章 | FAQ  | 新闻 | 下载 | 代码 | 工作 | 调查 | 术语 | 站点 | 图书 | 论坛 | 帮助 | 全部  

TOP | 交流 | 软件 | 专栏 | 开源 | 译/著 | 源码 | API  | 推荐 | FTP  | 积分 | 统计 | 搜索 | Blog | 我们  
首页 » 研究文集 » Java语言深入 搜索标题相关文章 搜索标题相关文章    评论此文章 发表评论     开始监控此文章 开始监控   加入收藏夹  加入收藏夹
如何使用Runtime.addShutdownHook(Thread)?
variable 原创   更新:2006-12-21 12:16:48  版本: 1.0   

以前从未用过 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


版权声明   给作者写信
本篇文章对您是否有帮助?  投票:         投票结果:     13       1
作者其它文章: 作者全部文章
评论人:zhengsheng 发表时间: Thu Dec 21 16:12:33 CST 2006
还真是没用过啊
评论人:airenhaoma 发表时间: Fri Dec 22 20:45:11 CST 2006
看来还真是有用呀
评论人:xmanlandauchosxx 发表时间: Sun Dec 24 14:48:45 CST 2006
正片文章,作者有浅到深,例子也很好[shine]
评论人:largefish 发表时间: Tue Dec 26 00:06:22 CST 2006
很不错

这个文章共有 4 条评论
主题: 敏捷开发技巧-消除代码异味 上一篇文章
返回文章列表 返回〔Java语言深入〕
下一篇文章 主题: UTF-8 字符集基础


文字广告链接
        自主、快速定制基于JAVA的B/S业务系统          重量级企业在线自定义WEB报表平台
        Excel制表、零代码发布、打印、图表结合——快逸报表,免费、稳定、功能强大的java工具
        技术圈: 关于Java、dotNet、PHP、Ruby、奇客、Web2.0等更多资讯博客精选文章

关于 JR  |  版权声明  |  联系我们 

©2002-2006 JR 版权所有 沪ICP备05019622号