Akka调度程序()每次重复都会延迟 [英] Akka scheduler() is late on every repeat

查看:367
本文介绍了Akka调度程序()每次重复都会延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的调度程序,每1秒重复一次任务:

I have simple scheduler for repeating task every 1 second:

Cancellable task = Akka.system().scheduler().schedule(
        Duration.create(0, TimeUnit.MILLISECONDS),
        Duration.create(1, TimeUnit.SECONDS),
        actor, new TickMsg("Tick", 0, 120)
);

不幸的是,股票代码持续时间所以最后演员在100毫秒之后收到了TickMsg - 好的,这在文档中描述并且我很清楚:

Unfortunately every pass is late for the ticker-duration so finally actor receives TickMsg exactly 100 ms later - ok, that's described in the doc and it's clear to me:


它不会在确切的时间执行任务,但是在每个刻度线上,它将运行过期的所有内容。

我无法理解的是为什么每个传递都迟到了,事实上这意味着每次传递1000ms需要1100ms。在10次通过后的结果中,我们有1秒延迟,1分钟后6秒,1小时后6分钟等...

What I can't understand is why every pass is late, which de facto means that every pass instead 1000ms needs 1100ms. In result after 10 passes we have 1 second of delay, after 1 minute it's 6 seconds, after 1 hour it's 6 minutes etc...

一些解决方案是设置重复持续时间稍微短一点,所以它在所需的时间点没有迟到,一个有效的例子,然后调度程序根据需要重复任务:

Some solution is setting repeat duration little bit shorter, so it's not late at required tick, for an example that works and then scheduler repeats tasks as required:

Cancellable task = Akka.system().scheduler().schedule(
        Duration.create(0, TimeUnit.MILLISECONDS),
        Duration.create((1000 - tickerDuration/2), TimeUnit.MILLISECONDS),
        actor, new TickMsg("Tick", 0, 120)
);

不幸的是,这种方式有点不舒服,容易忘记,还有其他方法可以重复每一个任务x秒(或其他 TimeUnit ),而不将其转换为毫秒和缩短?

Unfortunately this way is little bit uncomfortable and easy to forget, is there any other way to repeat tasks every x seconds (or other TimeUnit) without converting it to milliseconds and shortening?

推荐答案

这在Akka的2.1版中得到修复。

This is fixed in version 2.1 of Akka.

原因很简单:HashedWheelTimer不知道重复的任务,所以任务需要重新安排,但是由于这种情况按照定义发生在滴答之后它总是会延迟并落入下一个桶中。修复方法包括漂移校正,参见这里了解详情。

The reason is quite simple: the HashedWheelTimer does not know about repeated tasks, so the task needs to reschedule itself, but since this happens by definition after the tick it will always be late and fall into the next bucket. The fix was to include a drift correction, see here for the details.

这篇关于Akka调度程序()每次重复都会延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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