Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From which thread should System.exit() be called in a Swing-app?

Tags:

java

swing

In a Swing-app is it okay to invoke System.exit() from any thread? (e.g. on the EDT?)

like image 777
Anon Avatar asked Jul 19 '10 14:07

Anon


People also ask

How do I exit a Swing application?

4) There are two options to close the Java Swing application one is EXIT_ON_CLOSE and the other is DISPOSE_ON_CLOSE. 5) DISPOSE_ON_CLOSE doesn't terminate JVM if any user thread is running. 6) You can also implement a window listener to implement your closing mechanism by using System. exit() in the Swing application.

Does System exit terminate all threads?

System. exit() kills all the other threads. What you wrote is correct but it doesn't have anything to do with thread interruption. If you interrupt a thread, it will not just stop.

How do you exit a java application?

If you want to close or terminate your java application before this, your only option is to use System. exit(int status) or Runtime. getRuntime(). exit() .

What does dispose () do in java?

dispose(); causes the JFrame window to be destroyed and cleaned up by the operating system. According to the documentation, this can cause the Java VM to terminate if there are no other Windows available, but this should really just be seen as a side effect rather than the norm.


2 Answers

Since the VM is terminated after the System.exit() call I don't think it makes any difference from which thread the call is being made.

like image 30
Christian Seifert Avatar answered Sep 19 '22 16:09

Christian Seifert


You should not be calling System.exit() if you can help it.

The best way to exit a java process is to let all threads exit normally. This will terminate the VM.

In your main JFrame, you should setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE).

Then you can call frame.dispose() to close the JFrame and exit the EDT.

like image 147
jjnguy Avatar answered Sep 18 '22 16:09

jjnguy