System.exit(0) in java

后端 未结 5 1854
北恋
北恋 2021-01-02 07:41

I am writing an application program using swing. I need to exit from the application by clicking the JButton for that can i use System.exit() or sh

5条回答
  •  -上瘾入骨i
    2021-01-02 08:07

    If you need to set an exit code for your application, you have to use System.exit (I think).

    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.

    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. 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...

提交回复
热议问题