简体   繁体   中英

Java secure application closing

Is it a secure (and right) way to close a big Java application like this? Let's say user hits a close action or exit.

AppView av = new AppView(); //suppose using mvc approach
for (Frame frame : Frame.getFrames()) {
    av.dispose();
}
System.exit(0);

Depends on your application. If you have a multithreaded environment, you also might want to tell other threads to stop (and give them some time to complete what they were doing, or at least save their state if necessary). Also think about daemon threads vs normal threads.

If you didn't launch any extra threads, I think this will be fairly OK. But again, if you have some window closing listeners installed, they might be doing some things that could eventually mess up with other frames. Again: it all depends on your application.

You can stop your application with or without System.exit(0); .This situation depends on the nature of your application, if there is more than one thread, if it use external resources, if it serve other application.So let's talk about just those cases.

Ressource management : whenever you open a resource you have to close before , Java 7 add a new feature for resource management to automatically close a resource using try-with-resources , check also this article . An example of the risk that may appear when a file resource is not closed is File looking (in some OS).

Serving other apps : In case that your application is designed to serve other application in some way such as a socket communication, in one hand, you have to handle the case of interrupting the communication in the protocol level, what should you tell to the connected application ,on the other hand , you have to manage the used resource as described before.A bad protocol conception may cause an infinite waiting.

Multithreading : If your application launch more than one thread, then you have to work on a central class that would manage the stop action of your application if a safe way, each of the started thread should be informed that the application will be stopped, then each of them have to process this request to prepare for the closing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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