Problem with killing all Threads
posted 3 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Well, I have a program with some Group Threads
I need some sort of emergacy button (like "q"),that when I press it on my Kayboard, will kill all Threads and terminant program - what is the best choose ?
old KeyEvent from AWT / Swing ?
I need some sort of emergacy button (like "q"),that when I press it on my Kayboard, will kill all Threads and terminant program - what is the best choose ?
old KeyEvent from AWT / Swing ?
posted 3 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Assuming you're running your program from a terminal, then you will need to terminate the process where the JVM is running. If you're running as a foreground process then Ctrl+C will do it, or if a background process then find the process id (pid) and kill it, e.g. ps -ef | grep java, find the pid of the process that is your running program, then kill <pid>, where <pid> is your actual pid. These examples are based on a Linux OS.
Tim Driven Development | Test until the fear goes away
posted 3 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Well, first, you can't kill all Threads in a JVM, because some of them belong to the JVM itself.
For your own threads, remember that Thread cancel() is deprecated, as it can lead to unpredictable results. As can any external cancelling of threads - you can end up with mangled files, possible dangling network connections, incomplete database operations and who knows what else.
So, in short, the cleanest solution is to give each of your threads a shutdown indicator that you can flip to tell the thread when it needs to shut itself down. Meaning that the thread itself needs to wake up periodically and check the shutdown indicator.
If you really need to terminate everything, rudely and abruptly, the System.exit() method can do that, but that should be your last resort.
For your own threads, remember that Thread cancel() is deprecated, as it can lead to unpredictable results. As can any external cancelling of threads - you can end up with mangled files, possible dangling network connections, incomplete database operations and who knows what else.
So, in short, the cleanest solution is to give each of your threads a shutdown indicator that you can flip to tell the thread when it needs to shut itself down. Meaning that the thread itself needs to wake up periodically and check the shutdown indicator.
If you really need to terminate everything, rudely and abruptly, the System.exit() method can do that, but that should be your last resort.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
posted 3 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Why are you using ThreadGroup? Are you writing an application container and you want to isolate the threads from each other somehow?
Anyway, the solution to your question depends on how "panicky" your emergency button needs to be.
Normally you'd just distribute an ExecutorService to all components of your application that need to run background tasks, and when you want to quit you just shutdown the executor and let all remaining tasks run their course.
However, if you REALLY want the application to terminate immediately, you could call System.exit(). If you don't even want shutdown hooks to run, you can call System.getRuntime().halt(). Using these methods is discouraged, because from your application's perspective it's like pulling the power plug.
Anyway, the solution to your question depends on how "panicky" your emergency button needs to be.
Normally you'd just distribute an ExecutorService to all components of your application that need to run background tasks, and when you want to quit you just shutdown the executor and let all remaining tasks run their course.
However, if you REALLY want the application to terminate immediately, you could call System.exit(). If you don't even want shutdown hooks to run, you can call System.getRuntime().halt(). Using these methods is discouraged, because from your application's perspective it's like pulling the power plug.
posted 3 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi,
How about setting the thread as daemon thread?
If the application terminates, the daemon thread will terminate as well.
How about setting the thread as daemon thread?
If the application terminates, the daemon thread will terminate as well.
| No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |












