• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Death of a thread

 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just noticed that I am starting a bunch of threads but dont have any provisions for them exiting the run method.

So I am asking if I no longer hold a reference to a thread, will it die before the JVM exits on its own? Off the top of my head I am going to say no because that seems like it would require stoping the thread. And the JVM can not know when its safe to do so!?
 
Henry Wong
author
Marshal
Pie
Posts: 21423
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mr. C Lamont Gilbert:
I just noticed that I am starting a bunch of threads but dont have any provisions for them exiting the run method.

So I am asking if I no longer hold a reference to a thread, will it die before the JVM exits on its own? Off the top of my head I am going to say no because that seems like it would require stoping the thread. And the JVM can not know when its safe to do so!?


Not exactly sure what you are asking, but if you are asking if the thread objects will be garbage collected, the answer is no. They won't be garbaged collected, because the system still holds a copy of all active threads. In fact, you can still a get reference to all the active threads from the thread class.

Henry
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So until I make it leave the run method, the thread of execution as well as the Thread object will both live forever? Does this make threads the root of the reachability tree?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the Thread javadoc regarding the daemon setting. The JVM exits when all non-daemon threads complete. That means if a non-daemon thread is running, the JVM has to keep running it even if the main() thread ended long ago. Running threads and all the objects on them can't be collected, I imagine because the magical black box of the thread manager has references to them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic