This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Death of a thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Death of a thread" Watch "Death of a thread" New topic
Author

Death of a thread

Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

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
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Death of a thread
 
Similar Threads
Thread Q from jexam
Are java threads, real threads?
Regarding autostart of a servlet
Thread synchronization + static variables + volatile variables
Callin start from run method In case of threads Problem