3

I have noticed that scheduler.schedule in Akka does not work for schedules less than 10ms. For example I have tried below and I only receive a message every 10ms. Is it possible to reduce this, ie increase the frequency?

Thanks Des

akka.actor.ActorSystem("Test").actorOf(Props(new akka.actor.Actor {

    val system = context.system
    import system.dispatcher

    system.scheduler.schedule(0 milliseconds, 2 milliseconds, self, "Test")

    def receive = {

        case msg => 
            println("Received[" + System.currentTimeMillis + "]: " + msg)
    }
}), "Test")
user79074
  • 4,937
  • 5
  • 29
  • 57
  • I don't think you can go for a higher resolution than 10ms easily. See http://stackoverflow.com/a/351921/3185992 where a blog entry is linked. For higher resolutions you have to sacrifice portability or might need to tweak system settings. You can go for a direct access to hardware (if available) but inside the JVM (doesn't matter for Scala, Akka, ... whatever) I doubt you can go for a higher resolution. – tilois Sep 15 '14 at 18:41

2 Answers2

6

The Scheduler is not optimised for very precise scheduling, instead it is optimised for low overhead scheduling of a large number of timers (as in - "each Actor in your system wants to have a scheduled event"). When you schedule something it lands in a "bucket" together with other tasks for such time-slot and will execute no-sooner-than the given timeout.

The default bucket resolution is 10ms, as you noticed. You can tweak this setting via akka.scheduler.tick-duration, but be aware that relying on it to be exact may not be the best idea (system specific wait, garbage collections etc etc).

You can read more details on this in this akka-user thread: Using Scheduler vs deadline to mimic some backend

Konrad 'ktoso' Malawski
  • 13,102
  • 3
  • 47
  • 52
0

You can use the https://github.com/typesafehub/akka-quartz-scheduler/ to go upto 1 millisecond.

The project is up to date with Akka 2.4.x.

Konrad 'ktoso' Malawski
  • 13,102
  • 3
  • 47
  • 52
Ravi Kiran
  • 1,139
  • 6
  • 10