scheduleExecutorService.scheduleAtFixedRate() 方法如何保证实时执行? [英] How does the method scheduledExecutorService.scheduleAtFixedRate() ensures real-time execution?

查看:59
本文介绍了scheduleExecutorService.scheduleAtFixedRate() 方法如何保证实时执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我每秒都在执行一项时间紧迫的任务,所以我比较了几种方法以找到确保我的任务真正在固定时间步长内执行的最佳方法.在计算所有方法的错误的标准推导后,似乎使用 scheduledExecutorService.scheduleAtFixedRate() 方法会导致最佳结果,但我不知道为什么会这样.>

有人知道这种方法在内部是如何工作的吗?例如,与简单的 sleep() 相比,它如何确保引用的任务确实在固定的时间步长内执行?

解决方案

普通"Java VM 无法对执行时间(以及调度时间)做出任何硬实时保证.如果你真的需要硬实时保证,你应该看看像 Java RTS.当然,在这种情况下,您也需要一个实时操作系统.

关于与 Thread.sleep() 的比较:与 Thread.sleep() 的(天真的)用法相比,scheduledExecutorService.scheduleAtFixedRate() 的优势在于它不受计划任务执行时间的影响.请参阅 ScheduledFutureTask.runPeriodic() 了解如何实现.

Because I'm executing a time critical task every second, I compared several methods to find the best way to ensure that my task is really executed in fixed time steps. After calculating the standard derivation of the error for all methods it seems like using the method scheduledExecutorService.scheduleAtFixedRate() leads to the best results, but I don't have a clue why it is so.

Does anybody know how that method internally works? How does it for example in comparison to a simple sleep() ensure that the referenced task is really executed in fixed time steps?

解决方案

A 'normal' Java VM cannot make any hard real-time guarantees about execution times (and as a consequence of that also about scheduling times). If you really need hard real time guarantees you should have a look at a real time VM like Java RTS. Of course you need a real time OS in that case too.

Regarding comparison to Thread.sleep(): the advantage of scheduledExecutorService.scheduleAtFixedRate() compared to a (naive) usage of Thread.sleep() is that it isn't affected by the execution time of the scheduled task. See ScheduledFutureTask.runPeriodic() on how this is implemented.

这篇关于scheduleExecutorService.scheduleAtFixedRate() 方法如何保证实时执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆