我有两个班级:
运行预定作业的主要类:
public class TimerTest {
public static void main(String[] args){
try {
File logFile = new File("timer.log");
TestTimerJob job = new TestTimerJob(logFile);
Timer timer = new Timer();
timer.scheduleAtFixedRate(job, 0, Integer.parseInt(args[0]) * 60 * 1000);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Job类只需将当前日期时间写入文件(timer.log)
public class TestTimerJob extends TimerTask {
private File logFile;
public TestTimerJob(File logFile) {
this.logFile = logFile;
}
@Override
public void run() {
try {
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
BufferedWriter writer = new BufferedWriter(new FileWriter(this.logFile, true));
writer.write(df.format(cal.getTime()));
writer.newLine();
writer.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
然后我用命令运行程序:
java -cp Test.jar test.TimerTest 1
这意味着安排上述工作每1分钟运行一次。
我让它连续运行大约12个小时,当看到timer.log时,我发现了这个:
2015/04/02 07:32:17
2015/04/02 07:33:17
2015/04/02 07:34:17
2015/04/02 07:35:17
2015/04/02 07:36:17
2015/04/02 07:37:17
2015/04/02 07:38:17
2015/04/02 07:39:17
2015/04/02 07:40:17
2015/04/02 07:41:17
2015/04/02 07:42:17
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:16
2015/04/02 08:02:17
2015/04/02 08:03:17
2015/04/02 08:04:17
2015/04/02 08:05:17
2015/04/02 08:06:17
2015/04/02 08:07:17
2015/04/02 08:08:17
不知怎的,它在2015/04/02 07:42:17
暂停,然后在2015/04/02 08:02:16
恢复,并立即运行所有失实的时间表。
您能解释一下这个问题是什么以及如何解决这个问题?我使用的是jdk 1.6.0_34。抱歉我的英语不好。