2014-10-15 4 views
1

몇 밀리 초마다 액터를 "호출"하고 싶습니다. 그러나 스케줄러가 정확하지 않다는 것을 알았습니다.akka 스케줄러의 정확도를 높일 수 있습니까?

여기, 인쇄 결과가 akka은 0,10,30에 덩어리에게 시간을 보이는 등 한편

Thread.sleep는 사실 매우 정확하다는 것을 보여줍니다 코드

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) 
    } 

입니다 1 초만 자면

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

스케줄러의 정확성을 향상시킬 수있는 방법이 있습니까?

답변

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 
} 
+0

합리적인 낮은'틱 duration'에 무엇을 바인딩을 참조하십시오? –

관련 문제