I make a java swing application with some threads. When I pressed 'x' button in window, all running threads stopped and application close properly. But I want to close the application from java code. I used this.dispose(); for close the application. But when I used that method, window is closed. But all threads are still running. How I closed the application properly. please help me.
Asked
Active
Viewed 2,787 times
2
-
1if you want to close whole application then, use System.exit(0) then also this frame will be closed – Mayank Tiwari Jul 16 '13 at 08:23
-
do also make the those extra threads as daemon thread – veritas Jul 16 '13 at 08:25
-
Thank u all of you for help me. System.exit(0); is worked as veritas says. – Kalana Sarange Jul 16 '13 at 08:39
-
@KalanaSarange : If you are using `JFrame`, try to add this line `frameReference.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)` now after pressing `X` Button, if still the situation arises, as described in the latter part of your question, then I guess, you need to revisit your code regarding threads. As your threads are not properly shutting down. – nIcE cOw Jul 16 '13 at 09:22
-
@KalanaSarange : Please refer to this answer regarding how to [close a Swing Application from the code](http://stackoverflow.com/q/258099/1057230) for more information – nIcE cOw Jul 16 '13 at 09:30
2 Answers
3
use System.exit(0);
causes JVM kills the application silently, OR make other threads as daemon
-
2Is this the proper way of closing an application ? I really don't think so :( It is very much like I go to my `Task Manager` and give command to kill my `process` – nIcE cOw Jul 16 '13 at 09:20
-
-
@user2511414 : Please refer to the link I provided as a comment to OP's question. – nIcE cOw Jul 16 '13 at 09:31
-
@nIcEcOw it's about disposing the frame and gui components, not about other running threads :) – Jul 16 '13 at 09:40
-
@user2511414 : Please see the title of the question... Your way is definitely not the right way then, if you considering threads. (That thingy wait for all the threads to be closed, before terminating the JVM). – nIcE cOw Jul 16 '13 at 09:43
-
@nIcEcOw _I make a java swing application with some *threads* . . . window is closed. But *all threads* are still running_ :D – Jul 16 '13 at 09:45
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33546/discussion-between-nice-cow-and-user-2511414) – nIcE cOw Jul 16 '13 at 09:57
-
@user2511414 : To me it implies that, there lies the problem with the OP's code. So using `System.exit(0)`, the OP is not graciously closing the application. Can't say much in the absence of that code. – nIcE cOw Jul 16 '13 at 10:00
-
@nIcEcOw & user2511414 : According your conversation, I was really confused. because of nIcEcOw says System.exit(0) is like a process killer. then what should I do now? I don't know how to make the threads as daemon too. please give me some advise to resolve my problem. – Kalana Sarange Jul 16 '13 at 17:42
-
@KalanaSarange : Please have a look at this [example](http://stackoverflow.com/a/15676695/1057230), might be it can give you that slight idea, about the difference between the two approaches. Try to close the application after starting the thread, from the `X` button and then, first stop the thread and then press that `X` button :-) And do see how your command prompt reacts in both these situations :-) – nIcE cOw Jul 16 '13 at 17:58
-
@KalanaSarange : Instead try this [example](https://www.dropbox.com/s/tbmifx4upc3tq2t/ThreadCounter.java), it is just a slight improved version of the latter, with text colours to tell the difference that the two threads are running :-) And yeah, do try to change `JFrame.DISPOSE_ON_CLOSE` to `JFrame.EXIT_ON_CLOSE`, in the example and do see the difference, after applying the same closing way, as explained before. `JFrame.EXIT_ON_CLOSE` is same as using `System.exit(0)` – nIcE cOw Jul 16 '13 at 18:07
-
1@KalanaSarange for daemon thread, take look at [this](http://www.codeproject.com/Articles/616109/Java-Thread-Tutorial#dtr) buddy, always beware about the `System.exit();` because if your thread(s) is writing or saving something, then an inconsistent state will appear, otherwise feel free to kill them, while JVM kills daemon silently if there is no ant thread available – Jul 16 '13 at 18:24
1
See public static void exit(int status)
Terminates the currently running Java Virtual Machine.
But be careful when you use this in multi-threaded application.

Maroun
- 94,125
- 30
- 188
- 241