• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

how to kill main thread and relative subthread?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
i have a thread and in this thread i create other thread (not child of the first thread), if i kill the main thread how i automatically kill the related thread?
thanks in advance.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm just curious, how do you kill the main thread? Thanks.
 
m baldo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in general, my program use servlet(Record) and every request to a servlet is handled by a new thread of Record.
In the doget or dopost method of that servlet i launch a java program that continuously return me some data.
Using the servlet context i save a unique ID per Record thread and with this ID i take the right servlet thread in another servlet, called stop, then i call the interrupt method on it. I can stop the main thread, but the thread of the java program that i have created into Record thread continue his work and i can't figure out how to stop it.
Any ideas?
Thanks
 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not use System.exit(0) ?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Why not use System.exit(0) ?



System.exit() would potentially kill the entire Servlet Engine/server - bringing down multiple applications. Not something you want to do (and often blocked by a Servlet Engine's security manager).

Threads are not really related to each other, once you start them you can not expect there to be a relationship between the new Threads and the Threads that created them. There are a couple of work-arounds:
1) Use a ThreadGroup. Put all of the new Threads you want to cancel with the main one into a single ThreadGroup, then call ThreadGroup#interrupt() or iterate over the Threads in the group and interrupt them individually.

2) Collect all of the related Threads into some collection that is accessible to the 'main' thread when it gets interrupted. Then iterate over those Threads and interrupt them.

3) If you just want a safe way to stop a single web application in a server, you Server provider may have an admin console built in, you might try using that.

For the first two options you might have your Servlet Engine's security policy to contend with.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another option would be use - Executor class of java 5.






Difference Between Waiting and Yielding
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sunny Jain wrote:Another option would be use - Executor class of java 5.




Actually, that just shuts down the executor, preventing it from taking new tasks. It does not stop currently running threads. To stop currently running threads you could do this:
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic