File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes When do threads die Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "When do threads die" Watch "When do threads die" New topic

When do threads die

Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Normally, I let my threads run method end and that is the end of the thread. However, what if my main method is running or waiting in the run method and the thread object itself becomes no longer reachable, and it is a daemon thread. Will this thread end, or will it stay and wait until it is allowed to leave the run method before it ends.

Logically it seems like the thread must wait forever, even if there are no other references to it. This being the case it appears that a running/waiting thread can serve to make some class instances live forever unless the thread is allowed to exit.

Is this true? I could not find good info in jvm spec or JLS about when threads can end. Only info about locks and whatnot...
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

No running thread is ever unreachable, as a running thread always belongs to a ThreadGroup; this is by design.

While the JVM is up, no thread will ever just die because it is unreachable or for any other reason other than its run() method terminating, either normally (via a return) or abnormally (via an exception.)

It's certainly true that a running yet forgotten thread could be solely responsible for keeping lots of garbage alive.

[Jess in Action][AskingGoodQuestions]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
And at the specification level, JLS 12.6.1: "A reachable object is any object that can be accessed in any potential continuing computation from any live thread." A live thread is always reachable to itself, therefore it cannot be collected.
[ July 25, 2006: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Clear as day.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
So did you make a program like my first Wiki prototype that could only be stopped with KILL or Ctrl-Break? Ooops.

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
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Hehe, not quite that. I made a cacheing class that uses weak references. Then I made a thread to clean out the reference queue as objects appear. The net result is that the cacher object stays alive until all of the objects it was holding weakly are gone. I'd rather that object die when i no longer use it. Its really not that bad and will help a bit with diagnostics for it to live until the last of its weak references is cleared.
I agree. Here's the link:
subject: When do threads die
It's not a secret anymore!