Results 1 to 5 of 5
  1. #1
    guest_user is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Question if a thread is still running and there are no more references to it

    will it be garbage collected?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    A Thread is a root in terms of garbage collecting.
    Until the thread exits then anything that can trace a path to that Thread root is not eligible for garbage collection.
    Last edited by Tolls; 07-19-2011 at 02:45 PM. Reason: blimmin' typos

  3. #3
    guest_user is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    A Thread is a root in terms of garbage collecting.
    Until the thread exits then anything that can trace a path to that Thread root is not eligible for garbage collection.
    You mean that even if no one has references to a running Thread object, then the Thread object and all its references would not be garbage collected until the it returns?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Yes.
    The Thread object may well get garbage collected (it's merely a means of accessing a thread), but the thread it launched is still there running along merrily, just as the one that starts when the main() method is called.

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    If it's a daemon thread, and there are no non-daemon threads running, the JVM will kill it and exit.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Is it possible to keep a thread running?
    By Fortu in forum Threads and Synchronization
    Replies: 5
    Last Post: 01-07-2011, 06:36 PM
  2. How to keep a thread running
    By Fortu in forum New To Java
    Replies: 5
    Last Post: 12-31-2010, 09:45 AM
  3. Running a thread for a fixed period of time
    By ronyb85 in forum Threads and Synchronization
    Replies: 5
    Last Post: 12-16-2010, 07:26 PM
  4. Running thread a second time
    By Fleur in forum New To Java
    Replies: 18
    Last Post: 11-17-2009, 12:57 PM
  5. Running a thread on another appilcation
    By Charlie in forum Advanced Java
    Replies: 2
    Last Post: 08-12-2008, 12:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •