Scheduler working in actor system


I am using schedular to handle wait tasks. Say like I want to send a “foo” to testActor after 50 milliseconds. So I am using the below code.

    .scheduleOnce(Duration.ofMillis(50), testActor, "foo", system.dispatcher(), null);

I want to understand what it exactly doing ?
How it is triggering exactly after 50ms ?
And If I schedule another actor for 10000 ms what will happen ?
Only one thread handling for all scheduled messages or one thread per actor ?

please clarify the above points.

Hi there,

The Scheduler documentation provides answers to your questions in great detail. Including a link to the whitepaper that describes the inner workings, and a recommended alternative should you require a more reliable scheduling.

Hope this helps.

Thanks @chmodas

I have already went through that link you have shared.

These clarifications weren’t answered there. Kindly clarify me if you knows it in depth

Here’s what Akka has to say about that.

The scheduler in Akka is designed for high-throughput of thousands up to millions of triggers. The prime use-case being triggering Actor receive timeouts, Future timeouts, circuit breakers and other time dependent events which happen all-the-time and in many instances at the same time.

I can hardly add anything that will clarify it’s intent further. There’s a lot of stuff going on behind the scenes that relies on time and the scheduler makes sure these tasks are executed in timely manner (pardon the pun).

It actually does not trigger it after exactly 50ms.

The default implementation of Scheduler used by Akka is based on job buckets which are emptied according to a fixed schedule. It does not execute tasks at the exact time, but on every tick, it will run everything that is (over)due.

The default tick is set at 10ms - which is not going to be precise, mark you. So if schedule a task to run after 50ms, it will run in that vicinity :slight_smile:

As above, it will run about 10000ms later, give or take.

There’s a single thread that handles all of the scheduled tasks.

Please note that it is recommended to use Akka Timers instead of the system scheduler, should you wish to send delayed messages. Which are going to be just as precise as the scheduler ones.

1 Like

Thanks you @chmodas

In the doc it mentioned as

All scheduled task will be executed when the ActorSystem is terminated, i.e. the task may execute before its timeout.

In my case I am using Akka cluster(Cluster of actorsystems). So Is there any chance to schedule it into another actor system. So that I can avoid executing the message before its timeout

How will I ensure that it will be handled by a single thread or not ?

Does testActor potentially holds a thread for 100000 milliseconds ?