Scala 演员中的 Thread.sleep
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25789989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thread.sleep inside Scala actors
提问by As As
Is it correct to use Thread.sleep(5000);inside an actor? Does it actualy make an actor sleep for 5 seconds? Is there a simple alternative to make an actor sleep for some seconds?
Thread.sleep(5000);在演员内部使用是否正确?它真的能让演员睡 5 秒钟吗?有没有一种简单的替代方法可以让演员睡几秒钟?
回答by Chris K
Anything that blocks a thread is not advised within Akka. If the Actor is configured with a a shared thread pool (default behavior) then using Thread.sleep will withhold a thread from that pool that could be doing work for other Actors.
在 Akka 中不建议任何阻塞线程的东西。如果 Actor 配置了共享线程池(默认行为),则使用 Thread.sleep 将从该池中保留一个线程,该线程可以为其他 Actor 工作。
If one really must block, then an actor may be configured to have its own thread. This can be done by configuring a custom dispatcher for the actor to use, the full details are here.
如果确实必须阻塞,那么可以将actor 配置为拥有自己的线程。这可以通过为角色配置自定义调度程序来完成,完整的细节在这里。
The recognized alternative to blocking is to schedule a callback to the actor via a timer, for example send a message after 5 seconds..
公认的阻塞替代方法是通过计时器安排对 actor 的回调,例如在 5 秒后发送消息。
akkaSystem.scheduler.scheduleOnce(5 seconds, actor, "msgFoo")
The Akka scheduler is documented here: http://doc.akka.io/docs/akka/2.3.6/scala/scheduler.html
Akka 调度程序记录在此处:http: //doc.akka.io/docs/akka/2.3.6/scala/scheduler.html

