Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting reference to active threads

 
Deepak Nambiar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread.currentThread() should do the trick.
 
Deepak Nambiar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program 1 leaves each individual thread in infinite loop condition where each will be listening to a port.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic