简体   繁体   English

java中的System.exit(0)

[英]System.exit(0) in java

I am writing an application program using swing. 我正在使用swing编写应用程序。 I need to exit from the application by clicking the JButton for that can i use System.exit() or should i use some other methods, which is the best practice. 我需要通过单击JButton退出应用程序,因为我可以使用System.exit()或者我应该使用其他方法,这是最好的做法。 If calling System.exit() is not best practice then tell the reason and tell the alternative way to exit from the application. 如果调用System.exit()不是最佳实践,那么告诉原因并告诉退出应用程序的替代方法。

In general, calling System.exit(...) anywhere but the "main" method of an application can be problematic for (at least) the following reasons. 通常,在应用程序的“主要”方法的任何地方调用System.exit(...)可能会出现(至少)以下原因的问题。

  • It is an impediment to reusing your code. 重用代码是一个障碍。

  • It makes unit testing hard. 它使单元测试变得困难。 For example, if your code calls System.exit when your JUnit tests exercise some error handling, that it the end of your test sequence! 例如,如果您的代码在JUnit测试执行一些错误处理时调用System.exit ,那么它就是测试序列的结束!

The specific case of calling System.exit(...) in the button listener for the "exit" button is not so bad. 在“退出”按钮的按钮侦听器中调用System.exit(...)的具体情况并不是那么糟糕。 You are unlikely to want reuse the button listener somewhere that doesn't also require this behaviour. 您不太可能希望在不需要此行为的地方重用按钮侦听器。 Also, you can probably figure out a work-around for the unit testing conundrum; 此外,您可以找出单元测试难题的解决方法; eg don't unit test that particular method! 例如,不要对特定方法进行单元测试!

However, I think I'd still try to get the exit happen a different way. 但是,我想我仍然试图让退出以不同的方式发生。 For example, have the main method launch everything and then block on a CountDownLatch, then have the button listener decrement the latch. 例如,让main方法启动所有内容然后阻塞CountDownLatch,然后让按钮侦听器减少锁存器。 When the main method unblocks, it executes the relevant shutdown code and returns or exits as appropriate. main方法解除阻塞时,它会执行相关的关闭代码并根据需要返回或退出。

Personnaly, I do consider as best practice to let application exit by itself : if you write a main class with a main(String[] args) , with empty code, running it will exit silently from Java program. Personnaly,我认为最好让应用程序自行退出:如果你用main(String[] args)编写一个带有空代码的主类,运行它将从Java程序中静默退出。

But, in most times, like most of developpers, I rely upon System.exit(/ an exit code /) , especially for Swing applications, where the Swing EDT will run endlessly, as justkt wrote.. The known drawxback of this method is that most of applciation code is not called, what I avoid by setting a shutdown hook by calling Runtime.addShutdownHook(Thread) , which will allow me to clean application (close threads, and so on). 但是,在大多数情况下,像大多数开发人员一样,我依赖于System.exit(/ an exit code /) ,特别是对于Swing应用程序,其中Swing EDT将无休止地运行,就像justkt写的那样。这种方法的已知drawxback是大多数应用程序代码都没有被调用,我通过调用Runtime.addShutdownHook(Thread)来设置一个关闭钩子,这将允许我清理应用程序(关闭线程,等等)。

If you need to set an exit code for your application, you have to use System.exit (I think). 如果需要为应用程序设置退出代码,则必须使用System.exit(我认为)。

But when you do not need a specific code (or 0 is fine), then I would much rather have the JVM terminate "naturally", which happens when there are no more (non-daemon) threads left. 但是当你不需要特定的代码(或者0很好)时,我宁愿让“JVM”自然地终止,当没有更多的(非守护进程)线程时,就会发生这种情况。

Usually, you would just reach the end of your main method. 通常,您只需到达主方法的末尾即可。

I would be careful with System.exit because there may be other threads that still need to do something, and exiting without shutting them down properly may cause damage. 我会小心使用System.exit,因为可能还有其他线程需要做某些事情,并且在没有正确关闭它们的情况下退出可能会造成损害。 For example the embedded database might still need to flush its buffers. 例如,嵌入式数据库可能仍需要刷新其缓冲区。

If you know about these threads, you can usually arrange for them to end gracefully. 如果您了解这些线程,通常可以安排它们优雅地结束。 If you do not know what threads are running in your program, you have bigger problems... 如果您不知道程序中正在运行的线程,则会遇到更大的问题......

Generally speaking the only time you need to do a System.exit(n) is if you want to exit a command line program with an error code so that anything that is monitoring the program can detect the error code. 一般来说,只有在需要执行System.exit(n)时才需要退出命令行程序并使用错误代码,以便监视程序的任何内容都可以检测到错误代码。

This is most useful for example, if you are writing a program designed to be run from a shell script. 例如,如果您正在编写一个旨在从shell脚本运行的程序,那么这是非常有用的。

As other replies say though, if you are using threads then you must make every effort to shut them down gracefully before considering a System.exit(n) 正如其他回复所说,如果您正在使用线程,那么在考虑System.exit(n)之前,您必须尽一切努力将它们优雅地关闭。

System.exit can be bad at least when you have multiple threads or resources that need to be closed properly (that live outside JVM). 至少当你有多个线程或资源需要正确关闭(存在于JVM外部)时,System.exit可能会很糟糕。 If your application isn't exiting when you think it should then you should find out what the problem is and not just call System.exit(). 如果您认为应用程序没有退出,那么您应该找出问题所在,而不仅仅是调用System.exit()。

The only reason for calling System.exit() would be to give some none standard exit code. 调用System.exit()的唯一原因是给出一些没有标准的退出代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM