Handle signals in the Java Virtual Machine

房东的猫 提交于 2019-11-30 01:43:43

问题


Is it possible to handle POSIX signals within the Java Virtual Machine?

At least SIGINT and SIGKILL should be quite platform independent.


回答1:


The JVM responds to signals on its own. Some will cause the JVM to shutdown gracefully, which includes running shutdown hooks. Other signals will cause the JVM to abort without running shutdown hooks.

Shutdown hooks are added using Runtime.addShutdownHook(Thread).

I don't think the JDK provides an official way to handle signals within your Java application. However, I did find this IBM article, which describes using some undocumented sun.misc.Signal class to do exactly that. The article dates from 2002 and uses JDK 1.3.1, but I've confirmed that the sun.misc.Signal class still exists in JDK 1.6.0.




回答2:


Perhaps Runtime#addShutdownHook ?



来源:https://stackoverflow.com/questions/40376/handle-signals-in-the-java-virtual-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!