JAVA虛擬機關閉鈎子(Shutdown Hook)


Java程序常常也會遇到進程掛掉的情況。一些狀態沒有正確的保存下來,這時候就須要在JVM關掉的時候運行一些清理現場的代碼。JAVA中的ShutdownHook提供了比較好的方案。

JDK提供了Java.Runtime.addShutdownHook(Thread hook)方法。能夠注冊一個JVM關閉的鈎子。這個鈎子能夠在一下幾種場景中被調用:

  1. 程序正常退出
  2. 使用System.exit()
  3. 終端使用Ctrl+C觸發的中斷
  4. 系統關閉
  5. OutOfMemory宕機
  6. 使用Kill pid命令干掉進程(注:在使用kill -9 pid時,是不會被調用的)
以下是JDK1.7中關於鈎子的定義:

    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)

首先來測試第一種,程序正常退出的情況:

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();
		}
	}
}
執行結果:

The Application is doing something
Execute Hook.....
如上能夠看到。當main線程執行結束之后就會調用關閉鈎子。

以下再來測試第五種情況(順序有點亂,表在意這些細節):

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的發生。

執行結果:

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執行結束之后推出調用關閉鈎子不同。

接下來再來測試第三種情況:

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)

執行結果:
能夠看到效果如預期。


還有幾種情況就不一一列出了。有興趣的讀者能夠試一下。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM