Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads and GC  RSS feed

 
Sudharsan Govindarajan
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a thread which has a run method like this


And this thread object is instantiated and started in a local method



Assuming the startThread method is called in some other method, what will happen to the Thread object 'tObj' after the end of execution of startThread method? Will 'tObj' thread continue to execute or could it be garbage collected by the JVM?

Thanks
Sudharsan
[ June 08, 2004: Message edited by: Sudharsan G'rajan ]
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will continue to execute.
 
Sudharsan Govindarajan
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh!
Thanks Jose!

-Sudharsan
 
vinay prasad
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The object still keeps running until the object is not reached by the any live reference.

GC of java uses various techniques such as mark and sweep/ refernence counting algorithms to check for the active references. in case the object is not reached by the live refernces object will Garbage collected.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vinay prasad:
The object still keeps running until the object is not reached by the any live reference.


Hi Vinay,

Welcome to JavaRanch!

I wanted to point out that as long as a Thread is running, it will be referenced by its ThreadGroup, and as long as a ThreadGroup contains at least one running thread it, in turn, is referenced by its parent ThreadGroup; therefore, Jose is quite correct in stating that the Thread will continue to run forever (until, of course, something happens to make it stop.) The important point is that under no circumstances will the GC collect a running Thread; it's designed that way quite deliberately.
 
Sudharsan Govindarajan
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest! That explains it well.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!