java虚拟机怎么关闭_JAVA虚拟机关闭钩子(Shutdown Hook)

欢迎支持笔者新做:《深刻理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。

8e5883b703d940048010c5a4.html

Java程序常常也会遇到进程挂掉的状况,一些状态没有正确的保存下来,这时候就须要在JVM关掉的时候执行一些清理现场的代码。JAVA中的ShutdownHook提供了比较好的方案。java

JDK提供了Java.Runtime.addShutdownHook(Thread hook)方法,能够注册一个JVM关闭的钩子,这个钩子能够在一下几种场景中被调用:微信

程序正常退出

使用System.exit()

终端使用Ctrl+C触发的中断

系统关闭

OutOfMemory宕机

使用Kill pid命令干掉进程(注:在使用kill -9 pid时,是不会被调用的)

下面是JDK1.7中关于钩子的定义:ide

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)测试

首先来测试第一种,程序正常退出的状况:spa

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的发生。code

运行结果:blog

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)

运行结果:

b0083eb6de9f7084a587f6a02581f648.png

能够看到效果如预期。

还有几种状况就不一一列出了,有兴趣的读者能够试一下。

欢迎支持笔者新做:《深刻理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。

8e5883b703d940048010c5a4.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值