In a Swing-app is it okay to invoke System.exit()
from any thread? (e.g. on the EDT?)
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.
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.
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() .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With