多线程程序中存在 System.exit(1),返回码为 0
我在多线程程序中调用了 System.exit(1)
。然而,程序有时会以返回代码 0 退出,而不是返回代码 1。我没有任何其他调用 System.exit()
并且我确信该程序没有干净地退出。原因是什么?我该如何避免?
请注意,该错误是间歇性的,我无法在单线程程序中重现相同的行为。
I have a call to System.exit(1)
in my multi-threaded program. However from time to time instead of return code 1, the program exits with return code 0. I don't have any other calls System.exit()
and I'm positive that the program doesn't exit cleanly. What can be the cause, and how can I avoid it?
Note that the error is intermittent and I cannot reproduce the same behavior in single threaded programs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
修改您的设计以执行更受控制的关闭。
不应期望在具有多个线程的应用程序中调用
System.exit()
会导致程序干净退出。您不应该调用 System.exit() 来离开程序,而应该向每个移动组件发送关闭消息并使用 Thread.join() 恢复您创建的任何线程。您的应用程序应该能够通过这种方式很好地关闭所有部分。主线程中的最后一个命令应该是返回退出代码。如果您只是调用 System.exit(),您会将所有这些关闭细节留给 JVM,而 JVM 只会采取严厉的方法并当场杀死所有内容。
您使用过 Runtime.getRuntime.addShutdownHook() 吗?对 System.exit() 的调用将调用可能安装的任何关闭挂钩,这可能会更改退出代码。
Modify your design to execute a more controlled shutdown.
There should be no expectation that calling
System.exit()
in an application with multiple threads would ever cause the program to exit cleanly.Rather than calling
System.exit()
to leave the program, you should send shutdown messages to each moving component and useThread.join()
to recover any threads you created. Your application should be able to shut down all pieces nicely this way. The final command in the main thread should be to return your exit code. If you just callSystem.exit()
, you're leaving all of these shut down details to the JVM, which is just going to take a heavy-handed approach and kill everything on the spot.Have you used
Runtime.getRuntime.addShutdownHook()
at all? A call toSystem.exit()
will invoke any shutdown hooks that may be installed and this could be changing the exit code.运行时的文档。halt< /a>(int) 关于其参数的说法如下:
所以也许有什么东西正在调用
Runtime.halt(int)
。在关闭钩子或终结器中?The documentation for Runtime.halt(int) says the following about its argument:
So perhaps something is invoking
Runtime.halt(int)
. In a shutdown hook or finalizer?我认为,如果您的 JVM 在
System.exit(1)
实际执行之前终止,则可能会发生这种情况。您认为这在您的系统中可能吗?要么,带有
System.exit(1)
的代码正在守护线程中执行,因此当所有其他活动(非守护线程)线程完成工作时,JVM 会干净地退出(或不干净地退出,就像您如果你的程序抛出异常,仍然可以获得 0 退出代码!)或者,正如 @Erick Robertson 所建议的,也许某些东西正在从钩子或其他东西修改退出状态,尽管我不确定这是如何可能的。
注:请忽略我之前的评论。调用
System.exit(1)
将终止所有当前正在运行的守护程序/非守护程序线程。I think the only way this may happen if your JVM terminates before
System.exit(1)
actually executed. Do you think this may be possible in your system?Either, the code with
System.exit(1)
is being executed in a daemon thread, and so when all other live (non-daemon) threads finish working JVM exits cleanly (or not cleanly, as you can still get 0 exit code if you program throws an exception!)Alternatively, as @Erick Robertson suggested, maybe something is modifying the exit status from a hook or something, although I am not sure how this is possible.
Note: please disregard my previous comment. Calling
System.exit(1)
will terminate all currently running daemon/non-daemon threads all-together.