I have a Timer class that continuously runs. When it reaches five seconds, I would like to change a boolean value that will be used by another class. However, since it has its own thread, the value of the variable is not changing and I am unable to retrieve the changed boolean value.
public class Time extends TimerTask {
Timer s = new Timer();
boolean bool;
int secondsPassed = 0;
Toolkit toolkit = Toolkit.getDefaultToolkit();
public Time() {
bool = false;
}
TimerTask task = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
secondsPassed++;
if(secondsPassed == 5) {
System.out.println("Seconds passed: " + secondsPassed);
toolkit.beep();
bool = true;
}
}
};
public void nextPanel() {
toolkit = Toolkit.getDefaultToolkit();
s.scheduleAtFixedRate(task, 1000, 1000);
}
public boolean throwbool() {
return bool;
}
}
How would I stop the thread so that after five seconds "bool" would become true and I can return the new value in my other class? The other class is a GUI so the changed value as a check to when to switch to another panel.