ScheduleAtFixedRate 慢/晚
我正在使用 ScheduledExecutorService
使用
scheduleAtFixedRate
方法每小时提供一次数据库更新。问题是它逐渐变晚 - 在长期服务中我一直在记录它并且每天大约一秒钟。
我做了一个小班只是为了检查这个方面 - 当 PC(运行 WinXP)上没有发生任何事情时似乎工作正常,但如果事情发生,它很快就会变得更晚。昨晚 18:00:00.5
是它的第一个日志,今天早上是 09:00:00.5
然后是 10:00:05.9
,11:00:26.8
、12:00:45.3
、13:01:07.8
...
我可以附上代码,尽管我的例子还不是最小的。
还有其他人经历过这个吗?有什么想法为什么这不能正常工作吗?
我可以想出很多方法来解决它,但我真的很想知道为什么它不像广告中那样起作用!
谢谢,迈克
I'm using a ScheduledExecutorService
to provide an update to a database every hour with the scheduleAtFixedRate
method. The problem is that it gradually gets later - in long service I've been logging it and it's about a second a day.
I made a small class just to examine this aspect - seems to work fine when nothing is happening on the PC ( running WinXP ) but if things are going on it rapidly gets later. 18:00:00.5
last night was its first log and this morning was 09:00:00.5
then 10:00:05.9
, 11:00:26.8
, 12:00:45.3
, 13:01:07.8
...
I can attach the code although my example isn't the smallest.
Anyone else experienced this? Any ideas why this isn't working properly?
I can think of lots of ways around it but I'd really like to know why it doesn't work as advertised!
Thanks, Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,这是正常的。使用
scheduleAtFixedRate
,如果有执行该任务的执行时间比其周期长,则后续执行可能会延迟开始。话虽这么说,我建议scheduleWithFixedDelay
。这将确保任务按照指定的延迟间隔执行。This is normal AFAIK. With
scheduleAtFixedRate
, If any execution of this task takes longer than its period, then subsequent executions may start late. That being said, I'd recommendscheduleWithFixedDelay
. This will ensure that tasks are carried out at the specified delay interval.