gpt4 book ai didi

java - 计时器在午夜后不启动

转载 作者:行者123 更新时间:2023-12-01 08:48:44 25 4
gpt4 key购买 nike

我有两个计时器。其中一个每 5 分钟运行一次,另一个是 10 分钟运行一次。一旦我运行该应用程序,它就可以完美运行,但在 11:50 后它会停止执行。

我的代码如下:

//At minute 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, and 55 past every hour from 0 through 4 and every hour.

for(int hrsTime=1;hrsTime<=4;hrsTime++){
System.out.println("hours start time : "+hrsTime);
_log.info("MrkRomeFiles hours start time : "+hrsTime);
start_1=0;
for(int i=0;i<11;i++){
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, hrsTime);//everyday 1,2,3 and 4 oclock
today.set(Calendar.MINUTE, start_1);
today.set(Calendar.SECOND, 0);
timer.schedule( new MrkRomeFiles(_env), today.getTime(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS));

System.out.println("minuts start time : "+start_1);
_log.info("MrkRomeFiles minuts start time : "+start_1);

start_1=start_1+5;
}
}

//At minute 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, and 55 past every hour from 8 through 23 every hour.
for(int startTime=8;startTime<=23;startTime++){
int start=0;
System.out.println("hours start time : "+startTime);
_log.info("MrkRomeFiles hours start time : "+startTime);
for(int i=0;i<11;i++){
System.out.println("minuts start time : "+start);
_log.info("MrkRomeFiles minuts start time : "+start);
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, startTime);
today.set(Calendar.MINUTE, start);
today.set(Calendar.SECOND, 0);
timer.schedule(new MrkRomeFiles(_env), today.getTime(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS));
start=start+5;
}
}

//this is for run every 10min
for(int startTime=0;startTime<=23;startTime++){
_log.info("oracle hour time : : "+startTime);
System.out.println("oracle hour time : "+startTime);
int startOraMinutes=0;
for(int i=0;i<6;i++){
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, startTime);
today.set(Calendar.MINUTE, startOraMinutes);
today.set(Calendar.SECOND, 0);
timer.schedule(new MrkOraFiles(_env), today.getTime(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS));

_log.info("oracle minuts time : "+startOraMinutes);
System.out.println("oracle minuts time : "+startOraMinutes);
startOraMinutes=startOraMinutes+10;
}
}

下面是打印的日志:

2017-02-28 23:40:00,556 [       MrkRomeFiles.java:Timer-0:600] - mrkRome file start execution at 2017/02/28 23:40:00
2017-02-28 23:40:00,616 [ MrkOraFiles.java:Timer-0:179] - mrkOra file start execution at : 2017/02/28 23:40:00
2017-02-28 23:40:00,627 [ MrkOraFiles.java:Timer-0:179] - mrkOra file start execution at : 2017/02/28 23:40:00
2017-02-28 23:45:00,557 [ MrkRomeFiles.java:Timer-0:600] - mrkRome file start execution at 2017/02/28 23:45:00
2017-02-28 23:50:00,557 [ MrkRomeFiles.java:Timer-0:600] - mrkRome file start execution at 2017/02/28 23:50:00
2017-02-28 23:50:00,617 [ MrkOraFiles.java:Timer-0:179] - mrkOra file start execution at : 2017/02/28 23:50:00
2017-02-28 23:50:00,627 [ MrkOraFiles.java:Timer-0:179] - mrkOra file start execution at : 2017/02/28 23:50:00

最佳答案

为什么不简单地将任务安排为每 5/10 分钟一次?

    Timer timer = new Timer();
TimerTask task = new TimerTask(){
@Override
public void run(){
System.out.println(System.nanoTime());
}
};

Calendar today = Calendar.getInstance();
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
timer.scheduleAtFixedRate(task, today.getTime(), TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES));

关于java - 计时器在午夜后不启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42524840/

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