Thread.sleep inside Scala actors

Is it correct to use Thread.sleep(5000); inside the actor? Is he an actor’s dream for 5 seconds? Is there a simple alternative to get an actor to sleep for a few seconds?

+7
scala concurrency actor
source share
1 answer

Anything that blocks the thread is not recommended in Akka. If the actor has a common thread pool configured (the default behavior), then using Thread.sleep will keep the thread from that pool, which could do the work for other actors.

If you really need to block, then an actor can have its own thread. This can be done by setting up a user manager for use by the actor, full details are here .

A recognized alternative to blocking is scheduling an actor’s callback via a timer, for example, sending a message after 5 seconds.

 akkaSystem.scheduler.scheduleOnce(5 seconds, actor, "msgFoo") 

Akka scheduler is described here: http://doc.akka.io/docs/akka/2.3.6/scala/scheduler.html

+20
source share

All Articles