JAVA虚拟机关闭钩子(Shutdown Hook)
生活随笔
收集整理的這篇文章主要介紹了
JAVA虚拟机关闭钩子(Shutdown Hook)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java程序經常也會遇到進程掛掉的情況,一些狀態沒有正確的保存下來,這時候就需要在JVM關掉的時候執行一些清理現場的代碼。JAVA中的ShutdownHook提供了比較好的方案。
JDK提供了Java.Runtime.addShutdownHook(Thread hook)方法,可以注冊一個JVM關閉的鉤子,這個鉤子可以在一下幾種場景中被調用:
下面是JDK1.7中關于鉤子的定義:
| 1 2 3 4 5 6 7 8 9 10 11 | ????public void addShutdownHook(Thread hook) 參數: ????hook - An initialized but unstarted Thread object 拋出: ????IllegalArgumentException - If the specified hook has already been registered, or if it can be determined that the hook is already running or has already been run ????IllegalStateException - If the virtual machine is already in the process of shutting down ????SecurityException - If a security manager is present and it denies RuntimePermission("shutdownHooks") 從以下版本開始: ????1.3 另請參見: ????removeShutdownHook(java.lang.Thread), halt(int), exit(int) |
首先來測試第一種,程序正常退出的情況:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package com.hook;? import java.util.concurrent.TimeUnit;? public class HookTest? {? ????public void start()? ????{? ????????Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {? ????????????@Override? ????????????public void run()? ????????????{? ????????????????System.out.println("Execute Hook.....");? ????????????}? ????????}));? ????}? ????public static void main(String[] args)? ????{? ????????new HookTest().start();? ????????System.out.println("The Application is doing something");? ????????try? ????????{? ????????????TimeUnit.MILLISECONDS.sleep(5000);? ????????}? ????????catch (InterruptedException e)? ????????{? ????????????e.printStackTrace();? ????????}? ????}? } |
運行結果:
| 1 2 | The Application is doing something? Execute Hook..... |
如上可以看到,當main線程運行結束之后就會調用關閉鉤子。
下面再來測試第五種情況(順序有點亂,表在意這些細節):
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.hook;? import java.util.concurrent.TimeUnit;? public class HookTest2? {? ????public void start()? ????{? ????????Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {? ????????????@Override? ????????????public void run()? ????????????{? ????????????????System.out.println("Execute Hook.....");? ????????????}? ????????}));? ????}? ????public static void main(String[] args)? ????{? ????????new HookTest().start();? ????????System.out.println("The Application is doing something");? ????????byte[] b = new byte[500*1024*1024];? ????????try? ????????{? ????????????TimeUnit.MILLISECONDS.sleep(5000);? ????????}? ????????catch (InterruptedException e)? ????????{? ????????????e.printStackTrace();? ????????}? ????}? } |
運行參數設置為:-Xmx20M ?這樣可以保證會有OutOfMemoryError的發生。
運行結果:
| 1 2 3 4 | The Application is doing something? Exception in thread "main" java.lang.OutOfMemoryError: Java heap space? ????at com.hook.HookTest2.main(HookTest2.java:22)? Execute Hook..... |
可以看到程序遇到內存溢出錯誤后調用關閉鉤子,與第一種情況中,程序等待5000ms運行結束之后推出調用關閉鉤子不同。
接下來再來測試第三種情況:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package com.hook;? import java.util.concurrent.TimeUnit;? public class HookTest3? {? ????public void start()? ????{? ????????Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {? ????????????@Override? ????????????public void run()? ????????????{? ????????????????System.out.println("Execute Hook.....");? ????????????}? ????????}));? ????}? ????public static void main(String[] args)? ????{? ????????new HookTest3().start();? ????????Thread thread = new Thread(new Runnable(){? ????????????@Override? ????????????public void run()? ????????????{? ????????????????while(true)? ????????????????{? ????????????????????System.out.println("thread is running....");? ????????????????????try? ????????????????????{? ????????????????????????TimeUnit.MILLISECONDS.sleep(100);? ????????????????????}? ????????????????????catch (InterruptedException e)? ????????????????????{? ????????????????????????e.printStackTrace();? ????????????????????}? ????????????????}? ????????????}? ????????});? ????????thread.start();? ????}? } |
在命令行中編譯:javac com/hook/HookTest3.java
在命令行中運行:Java?com.hook.HookTest3 ?(之后按下Ctrl+C)
運行結果:
可以看到效果如預期。
還有幾種情況就不一一列出了,有興趣的讀者可以試一下。
from:?朱小廝
http://www.importnew.com/22765.html
總結
以上是生活随笔為你收集整理的JAVA虚拟机关闭钩子(Shutdown Hook)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初探 MySQL 的 Binlog
- 下一篇: Java程序员从笨鸟到菜鸟之(七十二)细