是否有可能提高akka调度程序的准确性

时间:2014-10-15 05:15:02

标签: scala akka

我想每隔几毫秒“调用”一个演员。但是我发现调度程序不是很准确。

这是代码

  def work3(): Unit ={
    val rate = 12
    var n = 0
    val a =  system.actorOf(Props(new Actor{
      var t1 = System.currentTimeMillis()
      def receive= {
        case _ =>
          //context.system.scheduler.scheduleOnce( rate milliseconds,self, 1 ) // if I use sceduleonce it becomes even more inaccurate.
          val t2 = System.currentTimeMillis()
          println(t2-t1)  //print 10 or 30
          n += 1
          t1 = t2
      }
    }))
    system.scheduler.schedule(0 seconds,rate milliseconds,a,1)

    //a ! "start"
    Thread.sleep(1000*100)
    println(n)
  }

打印结果显示akka看起来像是0,10,30等等。

另一方面,Thread.sleep实际上非常准确,即使只睡1毫秒

  def work2(): Unit ={
    while (true){
      val t1 = System.currentTimeMillis()
      Thread.sleep(1)
      val t2 = System.currentTimeMillis()
      println(t2-t1) // always print 1
    }
  }

有没有办法提高调度程序的准确性?

1 个答案:

答案 0 :(得分:3)

是的。

参见configuration 部分调度程序

# Used to set the behavior of the scheduler.
# Changing the default values may change the system behavior drastically so make
# sure you know what you're doing! See the Scheduler section of the Akka
# Documentation for more details.
scheduler {
    # The LightArrayRevolverScheduler is used as the default scheduler in the
    # system. It does not execute the scheduled tasks on exact time, but on every
    # tick, it will run everything that is (over)due. You can increase or decrease
    # the accuracy of the execution timing by specifying smaller or larger tick
    # duration. If you are scheduling a lot of tasks you should consider increasing
    # the ticks per wheel.
    # Note that it might take up to 1 tick to stop the Timer, so setting the
    # tick-duration to a high value will make shutting down the actor system
    # take longer.
    tick-duration = 10ms

    # The timer uses a circular wheel of buckets to store the timer tasks.
    # This should be set such that the majority of scheduled timeouts (for high
    # scheduling frequency) will be shorter than one rotation of the wheel
    # (ticks-per-wheel * ticks-duration)
    # THIS MUST BE A POWER OF TWO!
    ticks-per-wheel = 512

    # This setting selects the timer implementation which shall be loaded at
    # system start-up.
    # The class given here must implement the akka.actor.Scheduler interface
    # and offer a public constructor which takes three arguments:
    #  1) com.typesafe.config.Config
    #  2) akka.event.LoggingAdapter
    #  3) java.util.concurrent.ThreadFactory
    implementation = akka.actor.LightArrayRevolverScheduler

    # When shutting down the scheduler, there will typically be a thread which
    # needs to be stopped, and this timeout determines how long to wait for
    # that to happen. In case of timeout the shutdown of the actor system will
    # proceed without running possibly still enqueued tasks.
    shutdown-timeout = 5s
}