ScheduledExecutorService 每次都开始晚

发布于 2024-11-07 15:00:17 字数 1664 浏览 2 评论 0原文

我正在编写一小段具有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与酒说心事 2024-11-14 15:00:17

忘记它吧。
现在看来有效了。我不知道为什么,因为我什么也没做。

Forget about it.
Now it seems to work. I don't know why because I did nothing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文