views:

120

answers:

4

Up to now I used my application as a stand alone product. So, when user pressed "Stop" button I called System.exit(0); and it was fine.

Now my application will be called (in a programmatic way) from another program. So, I afraid that System.exit(0); will kill not only my process but also the external software which started my program.

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

ADDED:

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

A: 

As long as your programm isn't sharing an application server with others, shuting down the VM by calling System.exit(0) terminates all threads.

From Javadoc System.exit Terminates the currently running Java Virtual Machine)

EDIT: If you want to do some clean up code before shutdown, http://java.sun.com/j2se/1.4.2/docs/guide/lang/hook-design.html

stacker
@stacker, do you mean that `System.exit(0)` will shut all threads down including thread of the external software (which started my program).
Roman
It will kill the JVM. So if the external software started your program by spawning a Java process, it will be fine. If the external software is Java code that simply creates an instance of your class(es) and runs them, then that external code will be killed too.
Andrzej Doyle
@Roman It depends on whether your starter is java app itself and how you invoke the GUI-App or is it a shell/batch or another executable. You could clearify this point, if none of the answers match you questions.
stacker
+2  A: 

If the threads you've launched are still processing then calling System.exit(0) will cause them to be killed. In some cases, this can leave your application in an inconsistent state. Imagine that the thread was saving a file for example.

You should ensure that the your threads are all 'happy' to die before calling System.exit.

One technique you can use for this with long running threads is poisoning. To do this you send the threads a message that they should now die gracefully - i.e. a poson message. Once they have all died, it is safe to call System.exit(0) to terminate the Swing event handling thread.

There a loads of different ways of implementing poisoning, you could just set a global flag variable that the threads check to see if they've been poisoned, or you could use the Java 5 threading libraries. Take a look at this Javadoc for example and you'll find references to this technique:

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

Steve Neal
A: 

There is on "one-size-fits-all" answer to this that's a drop-in replacement for System.exit, unfortunately.

You will generally need to set some kind of flag that signals to all of your threads that it is time to exit, and ensure that they check this flag regularly. This will let them clean up gracefully without stopping abruptly, and it also ensures the effects are limited to your own components. In this case your application's main thread would also observe the flag, wait for all the "worker" type threads to finish and would then return all the way up the stack until your application's entry point was reached.

This question is not too dissimilar to the deprecated Thread.stop (etc) methods, especially with regards to replacing System.exit with something more respectful. In that light, the why is Thread.stop() deprecated page may be useful reading.

Throwing an exception (a custom one called something like ApplicationStopException) to unwind the stack of the main thread is not such a bad idea; this prevents you from having to handle the special logic all over your code and instead lets the "message" propagate to the higher levels, where they can take whatever action is needed to exit your program gracefully.

Andrzej Doyle
A: 

I recommend you to do flagging to stop the thread so that the thread will know when it has to stop. For GUI and window, you can call frame.dispose().

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

vodkhang