How can I close my software in a safe way?

So far, I have used my application as a standalone product. Therefore, when the user clicked the Stop button, I called System.exit(0);and everything was fine.

Now my application will be called (programmatically) from another program. Therefore, I am afraid that System.exit(0);not only my process will kill, but also the external software that launched my program.

So, what is the correct way to disconnect my application if a corresponding request is received from external software? My application is a graphical application. So, I want to close the window, but I also want to close all the processes performed by my program.

ADDED:

To be more specific, I want to close all threads launched by my program. My program does not start any OS process or any other program.

+5
source share
4 answers

If the threads you started are still being processed, calling System.exit (0) will destroy them. In some cases, this may leave your application in a conflicting state. Imagine a stream was saving a file, for example.

You must make sure your threads are “happy” to die before calling System.exit.

, , - . , , .. . , , System.exit(0), Swing.

, , , , , Java 5. Javadoc, :

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/BlockingQueue.html

+2
0

"-- " , , System.exit, .

- , , , , . , , , . , "" , .

Thread.stop ( ..), System.exit - . Thread.stop() .

(, - ApplicationStopException), , ; "" , , .

0

, , , . GUI frame.dispose().

For System.exit (), I think that this will not affect the caller, you can try to understand what is the real effect, but, as other people have already recommended, do not call it directly like that, just let the threads stop by itself

0
source

All Articles