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 Garbage Collection  RSS feed

 
Andrew Kays
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can some one tell me if a Thread reference goes out of scope or I null it in the code does the thread die? or will it run until completetion?
i.e if there are no valid references to a thread how does the garbage collection routine free the memory used by the thread that is running? I've tried a few examples myself and it appears the thread continues until it completes, but with out knowing for sure that the GC has run (which I cant know!) then I cant prove it one way or another!

Thanks
 
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
Hi Andrew,

Welcome to JavaRanch!

As long as a Thread is running, it belongs to a ThreadGroup. The ThreadGroup will have a reference to the Thread; thus it won't be collected.
 
Andrew Kays
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. So it there an implicit thread group always in existance while the program is running i.e does my new thread belong to the main thread group?
Is it possible to access this threadgroup at runtime, I assume it would be. H

Thanks for your help
 
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
If you don't assign a Thread to a group, then it belongs to the parent Thread's group, so yes, by default, the main ThreadGroup. You can call getThreadGroup() on a thread to get its ThreadGroup.
 
Andrew Kays
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! It all makes sense now!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!