温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JAVA虚拟机怎么关闭钩子

发布时间:2022-01-06 15:57:21 来源:亿速云 阅读:107 作者:iii 栏目:大数据

本篇内容介绍了“JAVA虚拟机怎么关闭钩子”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

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

运行结果:

JAVA虚拟机怎么关闭钩子

可以看到效果如预期。

“JAVA虚拟机怎么关闭钩子”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI