`
heipark
  • 浏览: 2078066 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

记录Java ShutdownHook

    博客分类:
  • Java
 
阅读更多
public class TestMe {

	static boolean isNornamExit = true;

	private static final void shutdownCallback() {

		if (!isNornamExit) {
			System.out.println("exception.");
		} else {
			System.out.println("exit normally.");
		}

	}

	public static void main(String[] args) throws InterruptedException {

		Runtime.getRuntime().addShutdownHook(new Thread() {
			@Override
			public void run() {
				shutdownCallback();

			}
		});

		isNornamExit = false;
		System.exit(1);

	}

}

 

注意正常退出也会执行 shutdownCallback()方法,所以要有标识存储是否正常退出信息,然后根据标识决定hook 方法怎么实现。

 

--end

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics