Re: Stopping threads with Java2
Posted By:
Chilang_Shen
Posted On:
Monday, April 9, 2001 09:47 PM
Hello, I always stop a thread via the following way:
Construct a Runnable class instead of extends from Thread, like this:
public class MyThread implements Runnable{
boolean run_f=false;
Thread thisThread = null;
// ... your code
public synchronized void start(){
if (!run_f){
run_f = true;
thisThread = new Thread(this);
thisThread.start();
}
}
public synchronized void stop(){
if (run_f){
run_f = false;
thisThread = null;
}
}
public void run(){
while(run_f){
try{
do_process();
}catch(InterruptedException e){
}
}
}
}
then, when you invoke stop() the thread will stop (after a loop?).
Can it help you?
Good luck my friends!!!