加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 模拟时间和Akka调度程序

发布时间:2020-12-16 08:44:48 所属栏目:安全 来源:网络整理
导读:我正在使用Akka构建多代理模拟,因此希望比实时更快地运行模拟.具体来说,我想配置Akka调度程序,以便调度程序从一个预定事件前进到下一个(显然可能涉及事件之间明显不同的时间步长),而不是通过一些基础固定时间步骤前进. 换句话说,我希望调度程序的行为好像它
我正在使用Akka构建多代理模拟,因此希望比实时更快地运行模拟.具体来说,我想配置Akka调度程序,以便调度程序从一个预定事件前进到下一个(显然可能涉及事件之间明显不同的时间步长),而不是通过一些基础固定时间步骤前进.

换句话说,我希望调度程序的行为好像它是一种优先级队列,其中优先级由事件的模拟时间戳给出.

这个清楚吗?如果是这样,我想使用Actor系统的默认调度程序做什么?如果这是不可能的,那么我将如何使用现有的Akka组件来完成此任务.

解决方法

我不认为这是可能的akka??调度程序.从 documentation(强调我的):

Sometimes the need for making things happen in the future arises,and
where do you go look then? Look no further than ActorSystem! There you
find the scheduler method that returns an instance of
akka.actor.Scheduler,this instance is unique per ActorSystem and is
used internally for scheduling things to happen at specific points
in time
.

但是,您总是可以使用递归函数完成相同的操作.假设您的“实时”功能类似于:

def periodicFunction() : Unit = ???  //whatever you're doing to Agents

//periodicFunction is called every 10 seconds
actorSystem.scheduler().schedule(0 seconds,10 seconds)(periodicFunction())

您的模拟代码可能只是:

@scala.annotation.tailrec
def fasterThanRealTimeLoop(n : Int) = 
  if(n > 0) {
    periodicFunction()

    fasterThanRealTimeLoop(n-1)
  }

然后你可以模拟20次运行

fasterThanRealTimeLoop(20)

可以进一步包装此功能以封装两种可能性:

val realtimeMode : Boolean = ??? //some configuration setting

val periodicArgs : Either[FiniteDuration,Int] = 
  if(realtimeMode) Left(10 Seconds) else Right(20)

periodicArgs.left.foreach { period => 
  actorSystem.scheduler().schedule(0 seconds,period)(periodicFunction())
}

periodicArgs.right.foreach { count => 
  fasterThanRealTimeLoop(count)
}

此代码现在将根据配置设置调用正确类型的循环(定时或尽可能快).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读