如何以安全的方式关闭我的软件?

发布于 2024-08-29 21:45:12 字数 342 浏览 2 评论 0原文

到目前为止,我将我的应用程序用作独立产品。因此,当用户按下“停止”按钮时,我调用了 System.exit(0); ,一切都很好。

现在我的应用程序将从另一个程序调用(以编程方式)。因此,我担心 System.exit(0); 不仅会杀死我的进程,还会杀死启动我的程序的外部软件。

那么,如果收到来自外部软件的相应请求,关闭应用程序的正确方法是什么?我的应用程序是一个 GUI 应用程序。因此,我想关闭窗口,但我也想关闭程序执行的所有进程。

添加:

更具体地说,我想关闭由我的程序启动的所有线程。我的程序不会启动任何操作系统进程或任何其他程序。

Up to now I used my application as a stand alone product. So, when user pressed "Stop" button I called System.exit(0); and it was fine.

Now my application will be called (in a programmatic way) from another program. So, I afraid that System.exit(0); will kill not only my process but also the external software which started my program.

So, what is the correct way to shutdown my application if a corresponding request from an external software is received? My application is an GUI application. So, I want to close the window but I also want to close all processes performed by my program.

ADDED:

To be more specific, I want to close all threads started by my program. My program does not start any OS process or any other program.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

罪歌 2024-09-05 21:45:26

我建议您进行标记来停止线程,以便线程知道何时必须停止。对于GUI和窗口,您可以调用frame.dispose()。

对于System.exit(),我认为它不会影响调用者,你可以尝试看看真正的效果是什么,但正如其他人已经建议的那样,不要直接调用它,让线程自行停止

I recommend you to do flagging to stop the thread so that the thread will know when it has to stop. For GUI and window, you can call frame.dispose().

For System.exit(), I think it will not affect the caller, you may try to see what is the real effect but as other people already recommended, do not call it directly like that, just let the threads stop by itself

瀞厅☆埖开 2024-09-05 21:45:24

不幸的是,有一个“一刀切”的答案,它是 System.exit 的直接替代品。

您通常需要设置某种标志,向所有线程发出信号,表明该退出了,并确保它们定期检查该标志。这将使它们优雅地清理而不会突然停止,并且还确保效果仅限于您自己的组件。在这种情况下,您的应用程序的主线程也会观察该标志,等待所有“worker”类型线程完成,然后一路返回堆栈,直到到达应用程序的入口点。

这个问题与已弃用的 Thread.stop (等)方法并没有太大的不同,特别是在用更受尊重的东西替换 System.exit 方面。鉴于此, 为什么是 Thread.stop()已弃用的页面可能会很有用。

抛出一个异常(一种自定义的异常,例如 ApplicationStopException)来展开主线程的堆栈并不是一个坏主意;这可以防止您必须处理整个代码中的特殊逻辑,而是让“消息”传播到更高级别,在那里他们可以采取任何需要的操作来优雅地退出您的程序。

There is on "one-size-fits-all" answer to this that's a drop-in replacement for System.exit, unfortunately.

You will generally need to set some kind of flag that signals to all of your threads that it is time to exit, and ensure that they check this flag regularly. This will let them clean up gracefully without stopping abruptly, and it also ensures the effects are limited to your own components. In this case your application's main thread would also observe the flag, wait for all the "worker" type threads to finish and would then return all the way up the stack until your application's entry point was reached.

This question is not too dissimilar to the deprecated Thread.stop (etc) methods, especially with regards to replacing System.exit with something more respectful. In that light, the why is Thread.stop() deprecated page may be useful reading.

Throwing an exception (a custom one called something like ApplicationStopException) to unwind the stack of the main thread is not such a bad idea; this prevents you from having to handle the special logic all over your code and instead lets the "message" propagate to the higher levels, where they can take whatever action is needed to exit your program gracefully.

差↓一点笑了 2024-09-05 21:45:22

只要您的程序不与其他程序共享应用程序服务器,通过调用 System.exit(0) 关闭虚拟机就会终止所有线程。

来自 Javadoc
System.exit 终止当前运行的 Java 虚拟机)

编辑:
如果您想在关闭前执行一些清理代码,http://java.sun.com/j2se/1.4.2/docs/guide/lang/hook-design.html

As long as your programm isn't sharing an application server with others, shuting down the VM by calling System.exit(0) terminates all threads.

From Javadoc
System.exit Terminates the currently running Java Virtual Machine)

EDIT:
If you want to do some clean up code before shutdown, http://java.sun.com/j2se/1.4.2/docs/guide/lang/hook-design.html

微暖i 2024-09-05 21:45:20

如果您启动的线程仍在处理,则调用 System.exit(0) 将导致它们被终止。在某些情况下,这可能会使您的应用程序处于不一致的状态。例如,假设线程正在保存文件。

在调用 System.exit 之前,您应该确保您的线程都“乐意”死亡。

对于长时间运行的线程,可以使用的一种技术是中毒。为此,您向线程发送一条消息,表明它们现在应该优雅地死亡 - 即 Poson 消息。一旦它们全部终止,就可以安全地调用 System.exit(0) 来终止 Swing 事件处理线程。

实现中毒的方法有很多种,您可以只设置一个全局标志变量,线程会检查该变量是否已中毒,或者您可以使用 Java 5 线程库。例如,查看此 Javadoc,您将找到对此技术的引用:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/BlockingQueue.html

If the threads you've launched are still processing then calling System.exit(0) will cause them to be killed. In some cases, this can leave your application in an inconsistent state. Imagine that the thread was saving a file for example.

You should ensure that the your threads are all 'happy' to die before calling System.exit.

One technique you can use for this with long running threads is poisoning. To do this you send the threads a message that they should now die gracefully - i.e. a poson message. Once they have all died, it is safe to call System.exit(0) to terminate the Swing event handling thread.

There a loads of different ways of implementing poisoning, you could just set a global flag variable that the threads check to see if they've been poisoned, or you could use the Java 5 threading libraries. Take a look at this Javadoc for example and you'll find references to this technique:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/BlockingQueue.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文