gpt4 book ai didi

Java设置时间表并使用计时器以分钟为单位执行操作(连续运行)

转载 作者:太空宇宙 更新时间:2023-11-04 12:27:08 24 4
gpt4 key购买 nike

我想配置一个基于分钟进行计划的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

}

请帮忙:(

最佳答案

if (min == 17) {                      
System.out.println("doing the scheduled task at " + min);
this.cancel();
}

this.cancel() is the culprit

Here is the description

公共(public) boolean 取消()

取消此计时器任务。如果任务已经被调度为一次性执行但尚未运行,或者尚未被调度,则永远不会运行。如果该任务已被安排重复执行,则它将永远不会再次运行。

希望这会有所帮助

关于Java设置时间表并使用计时器以分钟为单位执行操作(连续运行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38258469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com