• Post Reply Bookmark Topic Watch Topic
  • New Topic

thread reference var set to null  RSS feed

 
sandy sean
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!

Thread t=new Thread();
t.start();
t=null;

for the above code, will the thread pointed to by t still continue running after assigning its reference variable to null (assuming that there is still code remained in its run() method)???

thanks...
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that is correct. Setting the reference to null doesn't effect the thread.
 
sandy sean
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Wouter Oet!!!

can you tell me reasons for this???
 
Henry Wong
author
Sheriff
Posts: 22861
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sandy sean wrote:
can you tell me reasons for this???


I am assuming you are asking why the thread is still unaffected, because you are assuming that the thread object will be garbage collected?


If you are.... the thread object will *not* be GC'ed. Remember that once the thread is started, there is a new root for the GC to search -- the stack for the new thread. Within that root, the thread object is reachable -- any thread can access its thread object via the currentThread() method (which is a static method); so somewhere, in the core library, a running thread can always reach its thread object.

Henry
 
sandy sean
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!