ScheduledExecutorService 每次都开始晚
我正在编写一小段具有 ScheduledExecutorService 的代码。
为此,我有以下代码:
Calendar fileTimestamp = Calendar.getInstance(); fileTimestamp.setTime(fileObj.getDateToCopy());
ScheduledExecutorService fileDispatcher = Executors.newScheduledThreadPool(5);
CopyTask copyTask = new CopyTask();
long schedulerTrigger = fileTimestamp.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
System.out.println("***[MAIN-INFO]["+fileObj.getFile_id()+"] fileTimeStamp:"+fileTimestamp.getTime());
System.out.println("***[MAIN-INFO]["+fileObj.getFile_id()+"] now
TimeStamp:"+Calendar.getInstance().getTime());
System.out.println("[MAIN-INFO]["+fileObj.getFile_id()+"]
scheduledTrigger [hh:mm:ss]:
"+formatDateDiff(schedulerTrigger) );
ScheduledFuture<?> result = fileDispatcher.schedule(copyTask,
schedulerTrigger, TimeUnit.MILLISECONDS);
当到达时间戳时,必须执行 copyTask。 问题是copyTask执行得很晚:每次大约延迟3分钟。 下面是消息:
***[MAIN-INFO][401850] fileTimeStamp:Tue May 17 17:09:38 CEST 2011
***[MAIN-INFO][401850] now TimeStamp:Tue May 17 17:07:38 CEST 2011
[MAIN-INFO][401850] scheduledTrigger [hh:mm:ss]: 0:2:0 (120000 ms)
正在运行的任务的消息:
[401850-INFO] Launch time:Tue May 17 17:09:38 CEST 2011
[401850-INFO] Current time:Tue May 17 17:12:04 CEST 2011
[401850-INFO] Start copying FILE_ID=401850
[401850-INFO] Copy file OK
对于当前时间,我在 CopyTask 中有此代码:
public void run() {
System.out.println("["+taskId+"-INFO] Current time:"+new Date());
}
我们可以看到启动时间(作业必须运行的时间)与当前时间不同(作业执行的实时时间)。
有人知道为什么跑这么晚吗?
I'm working on a small piece of code that has a ScheduledExecutorService.
To do that I have the following code:
Calendar fileTimestamp = Calendar.getInstance(); fileTimestamp.setTime(fileObj.getDateToCopy());
ScheduledExecutorService fileDispatcher = Executors.newScheduledThreadPool(5);
CopyTask copyTask = new CopyTask();
long schedulerTrigger = fileTimestamp.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
System.out.println("***[MAIN-INFO]["+fileObj.getFile_id()+"] fileTimeStamp:"+fileTimestamp.getTime());
System.out.println("***[MAIN-INFO]["+fileObj.getFile_id()+"] now
TimeStamp:"+Calendar.getInstance().getTime());
System.out.println("[MAIN-INFO]["+fileObj.getFile_id()+"]
scheduledTrigger [hh:mm:ss]:
"+formatDateDiff(schedulerTrigger) );
ScheduledFuture<?> result = fileDispatcher.schedule(copyTask,
schedulerTrigger, TimeUnit.MILLISECONDS);
The copyTask must me executed when we reach a timestamp.
The problem is that copyTask is executed very late: about 3 minutes late every time.
Here below the messages:
***[MAIN-INFO][401850] fileTimeStamp:Tue May 17 17:09:38 CEST 2011
***[MAIN-INFO][401850] now TimeStamp:Tue May 17 17:07:38 CEST 2011
[MAIN-INFO][401850] scheduledTrigger [hh:mm:ss]: 0:2:0 (120000 ms)
The messages of the running task:
[401850-INFO] Launch time:Tue May 17 17:09:38 CEST 2011
[401850-INFO] Current time:Tue May 17 17:12:04 CEST 2011
[401850-INFO] Start copying FILE_ID=401850
[401850-INFO] Copy file OK
For the current time I have this code in the CopyTask:
public void run() {
System.out.println("["+taskId+"-INFO] Current time:"+new Date());
}
We can see that the launch time (the time when the job it must run) is not the same with the current time (the real time when the job is executed).
Does anybody have any idea why is running so late ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
忘记它吧。
现在看来有效了。我不知道为什么,因为我什么也没做。
Forget about it.
Now it seems to work. I don't know why because I did nothing.