I've tried Timer
and TimerTask
and they don't seem to support rescheduling
Here's the code that runs the timer, the start method is called and throws a non-descriptive exception
import java.util.Date;
import java.util.Timer;
public class ClassExecutingTask {
long delay = readSettings()*1000; // Delay in milliseconds
Program task = new Program();
Timer timer = new Timer("timer");
// Read delay in seconds from the settings file
public int readSettings() {
return new Program().readSettings();
}
public void start() {
timer.cancel();
// Make new timer
timer = new Timer("timer");
delay = readSettings()*1000; // Delay in milliseconds
Date executionDate = new Date();
timer.scheduleAtFixedRate(task, executionDate, delay);
}
}
I also tried using ScheduledThreadPoolExecutor
, but ended up getting the following stacktrace which makes itself quite clear
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Task already scheduled or cancelled
at java.util.Timer.sched(Unknown Source)
at java.util.Timer.schedule(Unknown Source)