我有两个计时器。其中一个每隔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
答案 0 :(得分:1)
为什么不按每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));