• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The Thread object

 
ravinderSingh singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Is a thread only represented by a single Thread object?

In other words, when you invoke the Thread.currentThread() method does this method return a reference to the same Thread object that was used to create the thread?

One more thing, is the Thread object representing a thread ever garbage collected if there are no references to the object?

Thanks
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to your first two questions are "yes". There is a single Thread object for each thread.

"Live" Thread objects always belong to a ThreadGroup, which keeps references to them. Therefore, a runnable Thread can never be garbage collected. They become eligible for collection after they are stopped.
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ravinderSingh singh:

Is a thread only represented by a single Thread object?

In other words, when you invoke the Thread.currentThread method does this method return a reference to the same Thread object that was used to create the thread?


Yes.


One more thing, is the Thread object representing a thread ever garbage collected if there are no references to the object?


gc will not collecte Thread if Thread is working. Even when there is no references to the Thread object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic