Posted By:
Andrew_York
Posted On:
Friday, August 16, 2002 11:40 AM
I usually use a boolean to control the thread.
while (keepRunning)
{
//Do Work
}
Your stop method should set
keepRunning to false. This will allow a graceful exit. I think setting
runner to null just throws away your refrence (and perhaps will cause a memory leak as the thread can keep running.) You may also want to try:
public synchronized void start()
{
runner = new Thread(this);
keepRunning = true;
runner.start();
}
public sychronized void stop()
{
keepRunning = false;
runner.interrupt(); //Only if you want to wake up the thread early. i.e. Stop now instead at the end of the sleep.
System.out.println("Thread has been stopped!");
}
This is just threory. . . Hopefully it helps.