In a Swing-app is it okay to invoke System.exit()
from any thread? (e.g. on the EDT?)
There are Swing rules for the EDT thread to terminate normally.
The most important is to make sure that all frames have been disposed. Unfortunately, this may not be that straightforward if you use modal dialogs without parents because Swing will create an invisible parent frame for such dialogs.
In this case, you have to list all frames (you can use Frame.getFrames()
for that) and explicitly dispose()
them.
Of course you must ensure that no Thread
is alive (except daemons ones). Some libraries and even some API from JDK create non-daemon threads, that you have to shut down yourself.
Finally, and most importantly, not calling System.exit() won't work in a Java Web Start environment (take a look at this SO question to find more information).
So, in conclusion, my advice would be to actually call System.exit()
because you don't always know in which environment your application will be launched. But I would add an important point to that: make sure to have a single point from which exit is performed. Calling it from any thread will be OK.