• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

thread scope

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic