• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is Thread still Alive?

 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Runnable that has this in it:

Right after I start that thread, I start a loop that checks to see if it's alive, and if it is calls interrupt() on it. The loop will continue for a total of 5 seconds. Why does it continue looping for the full 5 seconds and the thread is always alive? I thought interrupting a thread with an empty catch, will make it exit the run, thus ending that thread. Why is it still alive?
Also, it DOES print "interrupted"!
[ February 12, 2003: Message edited by: Robert Paris ]
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The threads are also returning isAlive() as false, so why can't I destroy the ThreadGroup?
[ February 12, 2003: Message edited by: Robert Paris ]
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I got the answer! (I think if I post it here, I'll discover the answer eventually )
The problem is that there's a field inside ThreadGroup called nthreads which has the number of threads. The only way to decrement this is by calling remove(Thread t). HOWEVER, that method is package-level access. So we go back to the way we got the Threads in the first place (see my other post):

After each thread finishes cleanup (in native code I believe) you can now call destroy()! So you might want to have a loop (with a limiting time so it doesn't go forever in weird cases) that has a Thread.sleep() in it so you can wait and try again. It does take a few seconds.
Oh, bte, destroy() is a method on ThreadGroup! Calling destroy() on a Thread throws an exception. I hope someone other than just me enjoyed this bit of info.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not create your own ThreadGroup?
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you mean extend ThreadGroup? That wouldn't work, it's not a protected variable but private. If I were to create my own object, it would be incompatible with Thread.getThreadGroup();
 
Anshul Chhabra
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Referring to your post :
The threads are also returning isAlive() as false, so why can't I destroy the ThreadGroup?
[ February 12, 2003: Message edited by: Robert Paris ]
Firstly, I tried something similar to what you mentioned, and in my case, the isAlive() method keeps on returning true all the time.
Also, why are you thinking of destroying the ThreadGroup?
Ultimately (it takes a little time), however, the thread does indeed die out at the end.
And once the thread is interrupted, it does fall out (if it had an empty catch), only, if the interrupting thread is fast enough, it can interrupt more than once , but the interrupted thread actually seems to get interrupted only once.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!