This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring Boot in Practice and have Somnath Musib on-line!
See this thread for details.
Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Garbage collection -- thread object

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assume that th is an instance holding a thread object. th.start() causes the thread to start running and eventually complete its execution. The object reference by th is not accessable any more and is garbage collected when the garbage collecter runs.

True
False
Thanks
kanchan
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kanchan,
I believe the answer is false. The object is still referenced by the variable th. Even though it is done running you can always start it again. If, on the other hand, you say th = null; now there are no more variables that hold a reference to that object and it can be garbage collected.
hope that helps, if I'm wrong someone please correct me!!
Dave
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave, threads can't be restarted after their run method has completed execution. I believe the answer to Kanchan's question will still be false, though, since th will hold a reference to the thread regardless of whether it has already been run. It will continue to hold that reference until it has been set to null. Also note that if you set th to null while the thread is still running, it can't be garbage collected until it has stopped running.
 
kanchan chaudhary
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
kanchan
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rancher's
I agree with Anjella.
Infact, you can refernce the members of "th" even after
the <code>run() </code> has complited execution, but let me
re-state here thread cant be restarted once its run() method
has finished execution.
Ravindra Mohan
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can never be certain that anything WILL eventually be garbage collected. All you can say about anything is whether it becomes eligible to be garbage collected.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Angela Ann:
Dave, threads can't be restarted after their run method has completed execution.


Sorry my mistake. Was thinking of a Runnable object.
thanks for catching it.
Dave
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic