我不认为使用akka调度程序可以实现这个功能。从
文档中可以看到(重点是我的):
有时需要在将来发生一些事情,那么你去哪里找呢?不要再看ActorSystem了!在那里,您会发现scheduler方法,该方法返回一个akka.actor.Scheduler实例,该实例对于每个ActorSystem都是唯一的,并且用于内部安排在特定时间点发生的事情。
但是,您始终可以通过递归函数完成相同的事情。假设您的“实时”功能类似于:
def periodicFunction() : Unit = ??? //whatever you're doing to Agents
//periodicFunction is called every 10 seconds
actorSystem.scheduler().schedule(0 seconds, 10 seconds)(periodicFunction())
您的模拟代码可能如下所示:
Your simulation code could simply be:
@scala.annotation.tailrec
def fasterThanRealTimeLoop(n : Int) =
if(n > 0) {
periodicFunction()
fasterThanRealTimeLoop(n-1)
}
然后,您可以使用HTML保留20次模拟运行。
fasterThanRealTimeLoop(20)
这个功能可以进一步封装,以包含两种可能性:
val realtimeMode : Boolean = ???
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)
}
这段代码现在将根据配置设置调用正确类型的循环(定时或尽可能快)。