# Scheduler working in actor system

**URL:** <https://discuss.akka.io/t/scheduler-working-in-actor-system/4944>\
**Category:** Actors\
**Tags:** akka\
**Created:** [August 26, 2019, 9:54am UTC](https://discuss.akka.io/t/scheduler-working-in-actor-system/4944 "2019-08-26T09:54:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anilkumble](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@anilkumble](https://discuss.akka.io/u/anilkumble)\
**Post date:** [August 26, 2019, 9:54am UTC](https://discuss.akka.io/t/scheduler-working-in-actor-system/4944/1 "2019-08-26T09:54:06Z")

</div>

Hi,

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.

```auto
system
    .scheduler()
    .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.

---

<div class="post-metadata">

**Author:** ![chmodas](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/chmodas/32/1552_2.png) [@chmodas](https://discuss.akka.io/u/chmodas)\
**Post date:** [August 28, 2019, 10:13am UTC](https://discuss.akka.io/t/scheduler-working-in-actor-system/4944/2 "2019-08-28T10:13:07Z")

</div>

Hi there,

The [Scheduler](https://doc.akka.io/docs/akka/current/scheduler.html#introduction) 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.

---

<div class="post-metadata">

**Author:** ![anilkumble](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@anilkumble](https://discuss.akka.io/u/anilkumble)\
**Post date:** [August 28, 2019, 2:06pm UTC](https://discuss.akka.io/t/scheduler-working-in-actor-system/4944/3 "2019-08-28T14:06:57Z")

</div>

Thanks @chmodas

I have already went through that link you have shared.

> [@anilkumble](#):
>
> 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 ?

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

---

<div class="post-metadata">

**Author:** ![chmodas](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/chmodas/32/1552_2.png) [@chmodas](https://discuss.akka.io/u/chmodas)\
**Post date:** [August 28, 2019, 2:55pm UTC](https://discuss.akka.io/t/scheduler-working-in-actor-system/4944/4 "2019-08-28T14:55:13Z")

</div>

> [@anilkumble](#):
>
> I want to understand what it exactly doing ?

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

> [@anilkumble](#):
>
> How it is triggering exactly after 50ms ?

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 🙂

> [@anilkumble](#):
>
> And If I schedule another actor for 10000 ms what will happen ?

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

> [@anilkumble](#):
>
> Only one thread handling for all scheduled messages or one thread per actor ?

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

Please note that it is recommended to use [Akka Timers](https://doc.akka.io/docs/akka/current/actors.html#actors-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.

---

<div class="post-metadata">

**Author:** ![anilkumble](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@anilkumble](https://discuss.akka.io/u/anilkumble)\
**Post date:** [August 28, 2019, 3:01pm UTC](https://discuss.akka.io/t/scheduler-working-in-actor-system/4944/5 "2019-08-28T15:01:20Z")

</div>

Thanks you @chmodas

In the doc it mentioned as

```auto
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

> [@chmodas](#):
>
> There’s a single thread that handles all of the scheduled tasks.

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

> [@anilkumble](#):
>
> I am using schedular to handle wait tasks. Say like I want to send a “foo” to testActor after 100000 milliseconds. So I am using the below code.

Does testActor potentially holds a thread for 100000 milliseconds ?
