使用“TERM”的信号处理

编程入门 行业动态 更新时间:2024-10-28 16:24:43
本文介绍了使用“TERM”的信号处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个独立的应用程序,我必须通过确认对话框提示用户保存他在尝试通过 start - > shutdown 。

I have a standalone application in which I have to prompt the user with an confirm dialog box to save the changes made by him when he tries to shutdown the system by start-->shutdown.

我通过使用 signalhandlers 来了解我们可以做到的。 可以帮助我如何使用信号处理程序

I came to know that by using signalhandlers we can do it. Can some one help me how to use signal handlers

推荐答案

2012年5月更新(2年半之后)

Update May 2012 (2 and half years later)

Trejkaz 评论:

在当前版本的Java上,此信号处理代码失败,因为 INT 信号是由VM保留或操作系统。 此外,当某些内容请求应用程序关闭时,其他任何有效的信号名称都不会触发(我只是仔细测试了所有我能找到的内容......) b $ b关闭钩子大部分都有效但我们发现在我们的情况下它不会触发,所以下一步显然是在JVM的后面注册一个处理程序

On current versions of Java this signal handling code fails because the "INT" signal is "reserved by the VM or the OS". Additionally, none of the other valid signal names actually fire when something requests the application to close (I just painstakingly tested all of the ones I could find out about...) The shutdown hook mostly works but we find that in our case it isn't firing, so the next step is obviously to resort to registering a handler behind the JVM's back

章节集成信号和异常处理的 HotSpot VM故障排除指南提到信号 SIGTERM , SIGINT , SIGHUP 仅适用于Solaris OS和Linux。 仅提到Windows上的异常处理。

The chapter "Integrating Signal and Exception Handling" of the "Troubleshooting Guide for HotSpot VM" mentions the signals "SIGTERM, SIGINT, SIGHUP" only for Solaris OS and Linux. Only Exception Handling on Windows are mentioned.

原始答案(2009年9月)

Original answer (Sept 2009)

a ShutdownHook 应该能够处理这种情况

a ShutdownHook should be able to handle that case

Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { // what you want to do } }));

(告诫) 参见:

(with caveats) See also:

  • Java信号处理和终止(链接已死,请参阅存档或镜像)
  • java退出信号处理:
  • Java signal handling and termination (link is dead, see archive or mirror)
  • java exit signal handling:

作为简单信号处理的例证:

as an illustration of simple signal handling:

public class Aaarggh { public static void main(String[] args) throws Exception { Signal.handle(new Signal("INT"), new SignalHandler () { public void handle(Signal sig) { System.out.println( "Aaarggh, a user is trying to interrupt me!!"); System.out.println( "(throw garlic at user, say `shoo, go away')"); } }); for(int i=0; i<100; i++) { Thread.sleep(1000); System.out.print('.'); } } }

更多推荐

使用“TERM”的信号处理

本文发布于:2023-10-08 23:59:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474131.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:信号处理   TERM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!