System. Exit (0) in Java

I am writing an application using swing I need to exit from the application and click JButton to use system Exit (), or I should use another method, which is the best practice If you call system Exit () is not a best practice, so please tell the reason and tell the alternative method to exit the application

Solution

Personally, I think the best way is to let the application exit by itself: if you write a main class with main (string [] args) and use empty code, running it will silently exit from the Java program

But most of the time, like most developers, I rely on system Exit (/ an exit code /), especially for swing applications, where swing EDT will run endlessly, as written by justkt The known drawxback of this method is that most application code has not been called. I call runtime Addshutdown hook (thread) sets a close hook to avoid this situation, which will allow me to clean up the application (close threads, etc.)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>