`

java runtime 之 ShutDownHook

    博客分类:
  • java
阅读更多

 

根据 Java API, 所谓 shutdown hook 就是已经初始化但尚未开始执行的线程对象。在

Runtime 注册后,如果 jvm 要停止前,这些 shutdown hook 便开始执行。

 

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

 

很明显,这些 shutdown hook 都是些线程对象,因此,你的清理工作要写在 run() 里。

根据 Java API,你的清理工作不能太重了,要尽快结束。但仍然可以对数据库进行操作。

 

 

package dirk.runtime;

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 
	    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 { 
	        	System.out.println("jvm run run run");
	            Thread.sleep(5000);     // (-: give u the time to try ctrl-C 
	            System.out.println("jvm prepare to shutDown");
	        } catch (InterruptedException ie) {  
	            ie.printStackTrace();  
	        } 
	         
	        System.out.println(">>> Slept for 10 seconds and the main thread exited."); 
	    } 
}
分享到:
评论

相关推荐

    ShutdownHook-Java优雅停机解决方案.docx

    ShutdownHook-Java 优雅停机解决方案 ShutdownHook 是 Java 语言提供的一种钩子机制,当 JVM 接受到系统的关闭通知之后,调用 ShutdownHook 内的方法,用以完成清理操作,从而平滑的退出应用。这种有计划平滑的...

    Java应用中使用ShutdownHook友好地清理现场

    Java中得ShutdownHook提供了比较好的方案。  JDK在1.3之后提供了Java Runtime.addShutdownHook(Thread hook)方法,可以注册一个JVM关闭的钩子,这个钩子可以在以下几种场景被调用:  1)程序正常退出  2)使用...

    解析Runtime中shutdown hook的使用详解

    在Java编程中,`Runtime`类的`shutdownHook`是一个重要的特性,它允许开发者在Java虚拟机(JVM)即将关闭时执行清理任务。本文将深入解析`shutdown hook`的使用,以及它在不同场景下的应用。 首先,`shutdown hook`...

    kill命令在Java应用中使用的注意事项小结

    在Java应用中,特别是对于SpringBoot这类框架,开发者通常会注册一个`ShutdownHook`,这是一种在JVM退出前执行的回调机制。例如,在上面的代码中,当使用`Runtime.getRuntime().addShutdownHook()`方法注册了一个新...

    JAVA并发编程实践-线程的关闭与取消-学习笔记

    15. **关闭钩子(Shutdown Hook)**:通过`Runtime.getRuntime().addShutdownHook(Thread hook)`注册自定义的关闭钩子,这些钩子在JVM关闭时执行,用于清理资源。 16. **精灵线程(Daemon Thread)**:精灵线程是不...

Global site tag (gtag.js) - Google Analytics