I have a program written in Java which uses 3 threads. Everything is working fine in the IDE (Netbeans) and when I kill it it destroys all the threads. When I launch it from the command line and Ctrl-C it the threads keep running. What’s the best way to deal with this ?
Does it kill the main thread as I guess I can use flags after that?
1. You can have
some loop control mechanismfor your threads using boolean.2. Else try using
Daemon threads, So as there will be No Non-Daemon thread, the JVM will get closed.By defalut its Non-Daemon, but you can set the thread as Daemon before you call
start()method on the thread of execution…The JVM will terminate ONLY when all the non-daemon threads including the Main thread hasterminated.Eg: