我想配置一个基于分钟计划的java系统,但每次执行操作时,它都会停止,不会根据分钟运行下一个操作。
public static void main(String[] args) throws ParseException {
// TODO code application logic here
Timer timer = new Timer();
TimerTask tt = new TimerTask() {
@Override
public void run() {
Calendar cal = Calendar.getInstance(); //this is the method you should use, not the Date(), because it is deprecated.
int min = cal.get(Calendar.MINUTE);//get the hour number of the day, from 0 to 23
try {
if (min == 17) {
System.out.println("doing the scheduled task at " + min);
this.cancel();
}
if (min == 18) {
System.out.println("doing the scheduled task at " + min);
this.cancel();
}
} catch (Exception e) {
System.out.println("Error Occurs " + e.getMessage());
}
}
};
timer.schedule(tt, 1000, 1000);// delay the task 1 second, and then run task every one second
}
请帮助:(
答案 0 :(得分:0)
if (min == 17) {
System.out.println("doing the scheduled task at " + min);
this.cancel();
}
this.cancel()是罪魁祸首
Here is the description
public boolean cancel()
取消此计时器任务。如果任务已安排为一次性执行但尚未运行,或尚未安排,则它将永远不会运行。如果任务已安排重复执行,则它将永远不会再运行。
希望这会有所帮助