controlling threads threads in infinite loops
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Justin_Poyser
Posted On:   Wednesday, April 18, 2001 09:40 PM

I have a thread that runs infinitely with a check on a variable to determine when it should stop procesing. I'm stuck with how I should write the controlling program so that I can initiate the stop pocess on each of the threads when required. ie the main program contains the following...... MQOracle mqObj1 = new MQOracle(attrs,1); MQOracle mqObj2 = new MQOracle(attrs,2); MQOracle mqObj3 = new MQOracle(attrs,3); MQOracle mqObj4 = new MQOracle(attrs,4); Thread t1 = new Thread(mqObj1); t1.start(); Thread t2 = new Thread(mqObj2); t2.start(); Thread t3 = new Thread(mqObj3); t3.start(); Thread t4 = new Thread(m   More>>

I have a thread that runs infinitely with a check on a variable to
determine when it should stop procesing.

I'm stuck with how I should write the controlling program so that
I can initiate the stop pocess on each of the threads when
required.

ie the main program contains the following......

MQOracle mqObj1 = new MQOracle(attrs,1);
MQOracle mqObj2 = new MQOracle(attrs,2);
MQOracle mqObj3 = new MQOracle(attrs,3);
MQOracle mqObj4 = new MQOracle(attrs,4);

Thread t1 = new Thread(mqObj1);
t1.start();

Thread t2 = new Thread(mqObj2);
t2.start();

Thread t3 = new Thread(mqObj3);
t3.start();

Thread t4 = new Thread(mqObj4);
t4.start();

This starts each of the threads. Now what I need to do is
to look for some interrupt in the main controlling program to
stop each of the threads gracefully with the following code

System.out.println("Shutting all Threads down");
mqObj1.setState(STOP);
mqObj2.setState(STOP);
mqObj3.setState(STOP);
mqObj4.setState(STOP);

so how do I initiate the above code when I decide to
shut the threads down? Is there some kind of exception I can
look for ?
any ideas would be most helpful.

Also, this program will eventually become a Windows 2000 service,
so any advice I can get with this too would be most helpful

Thanks in advance

Justin.

   <<Less

Re: controlling threads threads in infinite loops

Posted By:   Alex_Chaffee  
Posted On:   Tuesday, April 24, 2001 06:57 AM

There's no short cut here. When you want to stop the threads, you stop the threads. Are you looking for a button press? A file value changed? A certain amount of time to elapse?



If you're looking for a calculation to end, then that calculation must set a value in a shared variable.



You can also have the threads die when they finish processing. Another thread can wait for this with join().



That's the thing about infinite loops -- they never stop :-)

About | Sitemap | Contact