确定应用程序是否正常关闭 [英] determine if an application is shutting down normally

查看:121
本文介绍了确定应用程序是否正常关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法确定jvm是否正常关闭? Shutdown hook只能生成一个线程,有没有办法确定JVM当时是正常存在还是异常存在?

is there a way to determing if a jvm is shutting down normally? Shutdown hook can only spawn a thread, is there a way to determine if the JVM is existing normally or abnormally at that time?

推荐答案

我记得很久以前被问过的类似的问题。一种可能的做法是使用 SignalHandler

I remembered a similar question being asked time ago. One possible course of action is the use of SignalHandler.

您可以阅读全文在这里。它似乎与IBM JVM有关,但我认为它对Java Hotspot同样有效。

You can read the full article here. It appears to be related to IBM JVM but I think it is equally valid for Java Hotspot.


Java的一个鲜为人知的特性是
应用程序安装
自己的信号处理程序的能力,通过sun.misc.Signal
类支持
。但是,在使用sun.misc包
中的
类时要小心,因为它包含可能在Java版本之间更改
的无证件
支持类。您可以
为JVM未使用的任何信号
安装Java处理程序。这些
信号处理程序类似于本机
处理程序,因为它们是在引发
a本机系统信号时调用的,但是
它们将始终作为单独的
Java线程运行。基本上,当一个
信号被提出,其中一个Java
信号处理程序可用时,JVM的
信号调度程序线程被唤醒
并通知该信号。信号
调度程序线程然后调用Java
方法为已安装的Java信号
处理程序创建并启动一个新的
线程。要编写一个Java信号
handler,定义一个
实现sun.misc.SignalHandler
接口的类,并使用sun.misc.Signal.handle(
)注册处理程序( )
方法。

A little-known feature of Java is the ability of an application to install its own signal handler, which is supported through the sun.misc.Signal class. However, use caution when using classes from the sun.misc package because it contains undocumented support classes that may change between releases of Java. You can install a Java handler for any signal that is not used by the JVM. These signal handlers are similar to native handlers because they're invoked when a native system signal is raised, but they will always run as a separate Java thread. Essentially, when a signal is raised for which a Java signal handler is available, the JVM's "signal dispatcher thread" is woken up and informed of the signal. The signal dispatcher thread then invokes a Java method to create and start a new thread for the installed Java signal handler. To write a Java signal handler, define a class that implements the sun.misc.SignalHandler interface and register the handler by using the sun.misc.Signal.handle() method.

这篇关于确定应用程序是否正常关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆