• 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

Getting reference to active threads

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread.currentThread() should do the trick.
 
Deepak Nambiar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic