user79074
user79074

Reputation: 5270

Can I schedule a task to run less than every 10 ms

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

Upvotes: 3

Views: 770

Answers (2)

Ravi Kiran
Ravi Kiran

Reputation: 1139

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.

Upvotes: 0

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

Upvotes: 6

Related Questions