java.util.Timer系统时间效果如何?

发布于 2024-11-30 13:22:20 字数 219 浏览 0 评论 0原文

说现在是6点,我有一个Timer并在10点安排了一个TimerTask。之后,系统日期时间被其他服务(例如ntp)调整为9点钟。我仍然希望我的 TimerTask 在 10 点触发,但事实并非如此,Timer 仍然等待接下来的 4 小时并触发我的 TimerTask 。在这种情况下我该怎么办?

Said now is 6 o'clock, I have a Timer and scheduled a TimerTask at 10 o'clock. After that, System DateTime is adjusted by an other service (ntp for example) to 9 o'clock. I still want my TimerTask will be fired at 10 o'clock but it does not, Timer still wait for next 4 hours and fire my TimerTask. What should I do in this situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

秋日私语 2024-12-07 13:22:20

首先,如果你的时钟慢了 4 小时,你就已经陷入了相当混乱的境地。通常,时间调整只会以毫秒或秒为单位,或者偶尔以一两分钟为单位(如果机器很长时间没有在线)。一种选择是在设置计时器之前进行自己的 NTP 调用来检查时间是否相当准确。

另一种选择是制作一个合理定期调用的计时器(例如每分钟或五分钟一次),它检查时间,然后有选择地采取行动。它的效率稍低,但我不认为唤醒单个线程以每分钟左右执行一次简单检查会对性能产生重大影响。您应该根据您需要计时器触发的准确程度以及您需要它对性能的影响有多大来调整检查的规律性。

Firstly, you're already in a pretty nasty mess if your clock is out by 4 hours. Typically time adjustments will only be by milliseconds or seconds - or occasionally a minute or two, if the machine hasn't been online for a very long time. One option would be to check that the time is reasonably accurate by making your own NTP call before setting the timer.

Another option is to make a reasonably regularly-invoked timer - for example once every minute or five minutes - which checks the time and then optionally takes action. It's slightly less efficient, but I wouldn't expect the impact of waking up a single thread to perform a simple check once a minute or so would have a significant effect on performance. You should adjust the regularity of the check based on how accurately you need your timer to fire, and how little performance impact you need it to have.

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