I have a Gui in which I have animation placed in the thread (Runner). I want the thread to stop when the user pushes the stop button (in a different panel than the animation. Originally I had set up a flag variable in the thread to make it stop until I realized that I would have to create checks for the variable and the animation would continue playing until it reached one of the check points. Is there a way/ another option to make the thread stop quickly. Thank you for any help you offer!
Below is my current code:
class Runner extends Thread {
public SimulationPanel sp;
public volatile boolean isRun;
public void run() {
while (!isRun){
System.out.println("is Run is " + isRun);
Boolean isVisible = true;
for(int i = 0; i<10; i++){
if (isVisible ==true) {
sp.setRepaintValue(SimulationPanel.RepaintType.REMOVERIGHTLINE);
isVisible = false;
delay(100);
}else if (isVisible == false){
sp.setRepaintValue(SimulationPanel.RepaintType.RIGHTLINE);
sp.repaint();
delay(100);
isVisible = true;
}
}
if(isRun) return;
......}
public void stopThread() {
System.out.println("stopThread method executing properly");
isRun = true;
}
And the Action Listener for the stop button:
else if (command.equals ("stop")) {
new Timer(true).schedule(new TimerTask() {
public void run() {
System.out.println("Requesting stop");
runnerThread.stopThread();}
}, 5);
}