• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Problem with killing all Threads

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Marshal
Posts: 6209
501
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 29001
214
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2466
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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
reply
    Bookmark Topic Watch Topic
  • New Topic