Forums Register Login

Death of a thread

+Pie Number of slices to send: Send
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!?
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
These are the worst of times and these are the best of times. And this is the best tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1026 times.
Similar Threads
Thread synchronization + static variables + volatile variables
Thread Q from jexam
Are java threads, real threads?
Regarding autostart of a servlet
Callin start from run method In case of threads Problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:14:15.