如何访问我的计时器安排的TimerTasks?我试图从我的taskMap中删除任务后取消任务。我已经尝试取消取消所有计划任务的计时器,然后我只需要在我的taskMap中使用更新的任务再次启动计时器,但是我收到了“IllegalStateException计时器已被取消”。以下是扩展TimerTask的类的代码:
public class ShowTask extends TimerTask {
private static Timer timer = new Timer();
private static String taskDescription;
private static Stage popupStage;
public void run() {
Platform.runLater(new Runnable() {
@Override
public void run() {
try {
Popup_Controller.taskDesc = taskDescription;
popupStage = new Stage();
popupStage.initStyle(StageStyle.UNDECORATED);
Parent root = FXMLLoader.load(getClass().getResource("/application/popupTask.fxml"));
Scene scene = new Scene(root,295,121);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
popupStage.setScene(scene);
popupStage.show();
Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
popupStage.setX(primScreenBounds.getWidth() - (popupStage.getWidth() + 10));
popupStage.setY(primScreenBounds.getHeight() - (popupStage.getHeight() + 10));
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static void runTask(String desc, int hour, int min) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
cal.set(Calendar.SECOND, 0);
long oneDay = 1000L * 60L * 60L * 24L;
Date timeExec = cal.getTime();
taskDescription = desc;
if (timeExec.before(new Date())) {
cal.add(Calendar.DATE, +1);
timer.schedule(new ShowTask(), cal.getTime(), oneDay);
Controller_main.taskMap.put(desc, timeExec);
} else {
timer.schedule(new ShowTask(), cal.getTime(), oneDay);
Controller_main.taskMap.put(desc, timeExec);
}
System.out.println(timeExec);
System.out.println(Controller_main.taskMap.entrySet());
}
public static void runTask(String desc, Date date) {
long oneDay = 1000L * 60L * 60L * 24L;
taskDescription = desc;
if (date.before(new Date())) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, +1);
timer.schedule(new ShowTask(), cal.getTime(), oneDay);
} else {
timer.schedule(new ShowTask(), date, oneDay);
}
}
public static Timer returnTimer() {
return timer;
}
public static Stage returnStage() {
return popupStage;
}
}