This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Getting reference to active threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Getting reference to active threads" Watch "Getting reference to active threads" New topic
Author

Getting reference to active threads

Deepak Nambiar
Greenhorn

Joined: Jan 17, 2011
Posts: 8
Guys, I am in a situation :

I have a Socket application which creates Threads of Socket Servers listening to each port on request. On each request I create a thread which will listen to a port in infinite loop ( like while(boolean var) ). But I have to address a situation in which a stop request will come for a port listener which needs to be processed. I need a reference to the active thread already created by which i can alter the boolean variable which causes infinite looping in respective threads. Guys , Please help me out how to achieve this.

PS : if my approach is wrong kindly pump in some ideas of achieving this.


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
Thread.currentThread() should do the trick.
Deepak Nambiar
Greenhorn

Joined: Jan 17, 2011
Posts: 8
both are not in same process. like creating threads for socket goes one way. and deactivation comes other way.its not like start --> do listen --> end.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
Sorry, don't understand what you mean by that reply.

If you want references to all threads you created, you have to store the references somewhere when you create them (like a List).
Deepak Nambiar
Greenhorn

Joined: Jan 17, 2011
Posts: 8
Scenario is Program 1 --> creates Listener in seperate Thread --> exit.
Program 2 ---> stop Listener --> exit

These 2 programs will be called at different times.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
If program 1 exits (by which I understand the JVM to quit), then all threads it creates are gone, too; I think I'm missing something.
Deepak Nambiar
Greenhorn

Joined: Jan 17, 2011
Posts: 8
The program 1 leaves each individual thread in infinite loop condition where each will be listening to a port.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
OK, so "exit" does not mean quit. What, then, is "program 2" - is it a piece of code that runs in the same JVM? Only then would it have a chance to do anything with the threads.
Either way, what I said in my second post still holds - if later on you plan to do something with those threads, then you need to store the Thread objects in some data structure that you can get at later.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Personally, I have used java.lang.ThreadGroup to keep track of Threads in this situation. I had the class which handles an established socket connection extend Thread so ThreadGroup automatically provided a data structure.

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting reference to active threads