• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to kill main thread and relative subthread?  RSS feed

 
m baldo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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.
 
T. Huy Nguyen
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Stephan van Hulst
Saloon Keeper
Posts: 7722
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not use System.exit(0) ?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option would be use - Executor class of java 5.






Difference Between Waiting and Yielding
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!