Harmeet Singh Taara
Harmeet Singh Taara

Reputation: 6611

Akka Scheduler: How akka scheduler work?

I am going to explore Akka Scheduler. As of my assumptions, the scheduler run the scheduled task within specific duration of time in another thread. According to the documentation, they are used Runnable object with in scheduler. This is not clear, why they are used Runnable object and without call start() on runnable, the new thread stack not produce. Or Scheduler doesn't create a septate thread for running scheduled task?

How akka scheduler work under the hood?

Upvotes: 1

Views: 2045

Answers (3)

pacman
pacman

Reputation: 837

When i used Akka Scheduler, i tried to research source code for obtaining more knowledge - https://github.com/akka/akka/blob/master/akka-actor/src/main/scala/akka/actor/Scheduler.scala

As you can see, Akka Scheduler uses ExecutorContect (wrapper under ForkJoinPool) as implicit parameter to onvoke new Runnableand sends message to another actors via receiver ! message by leveraging fire and forget pattern.

Another important classes for understanding the full picture are FiniteDuration (it allows to set up delay time), Cancellable (it allows to cancel the execution of the scheduled operation), they are using in the most methods related with scheduling processes (e.g. scheduleOnce, i used this for time synchronization between actors)

Upvotes: 2

thibr
thibr

Reputation: 617

There is no start() on Runnable. You are mixing Thread and Runnable. Under the hood, the Scheduler use a Dispatcher, which can be viewed,to simplify, as an akka layer on top of some ThreadPool. To execute you task, akka will submit your task (the Runnable) to this pool, then the pool will have one his Thread to call the run() method of the task.

Upvotes: 0

Related Questions