Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

thread scope  RSS feed

 
Marcin Wojcik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Everybody,

Can anybody help me understand what happens if following block is executed and we go outside of its scope (assume I have some MyThread class that extends Thread and its run() method runs forever):

{
Thread t = new MyThread();
t.start()
}


Now MyThread is running but I no longer have a reference to it. Is there a way to get that reference somewhere else (without using global variable)? Can I name a thread and look it up somehow in some other place in my code? Also I'm a bit confused as to what happens to reference t - is it available for garbage collection immediately once the block above is done? I have seen people setting t = null immediately after t.start(). What is that doing? I don't think it has any effect on the thread itself, has it?
 
Arka Guhathakurta
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
It is one of the Java Ranch policies to ask one question per post. Now coming to your question....
and we go outside of its scope
. I did not understand what did you mean by we. If the run method of your thread runs infinitely like
Your program will never terminate.
Yes you can give a name to your thread either through the thread constructor or by invoking the method setName().
Let us see the following code...

Garbage Collector is a low-priority daemon thread whose sole work is acquisition of High-priority threads. I won't go into the details of what a daemon thread is because it is a little more complex than normal user threads. Once a thread or any object becomes eligible for Garbage Collection the memory will eventually be reclaimed..it is only a matter of time. I would request you to go through Mutli-threading concepts that will clear your doubts further.

Always remember you cannot force garbage collection. You can only make objects eligible for garbage collection.

Henry Wong is the best person to answer threading. I believe i could clear your doubts.Hope it helps.
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as the second question is concerned you could have very easily found the answer by looking at the JavaDoc or doing a Google search. You can lookup a thread by name or id.

If I am not mistaken by scope you mean scope of variable t in your code. Setting t=null is will not make the thread garbage collectable due to the fact that the ThreadGroup object is holding on to a reference to your thread. If you look inside the code for Thread.start() there is a call to group.add(this).
 
Marcin Wojcik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Appreciate your replies and apologies for multiple questions in one shot. That won't happen again

I can see how I could use threadGroup to get a list of running threads and look through it to find the thread with a name I want (Thread.currentThread().getThreadGroup().enumerate(list) where list = Thread[])

Thanks,
Marcin
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcin Wojcik wrote:
I can see how I could use threadGroup to get a list of running threads and look through it to find the thread with a name I want (Thread.currentThread().getThreadGroup().enumerate(list) where list = Thread[])


That will only work if the Thread you are looking for is in the same ThreadGroup or in a ThreadGroup that is a child of the current ThreadGroup. for a better solution have a look at http://nadeausoftware.com/articles/2008/04/java_tip_how_list_and_find_threads_and_thread_groups
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!