Thread.sleep里面Scala演员 [英] Thread.sleep inside Scala actors

查看:187
本文介绍了Thread.sleep里面Scala演员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个actor中使用 Thread.sleep(5000); 是否正确?它实际上使演员睡5秒?是否有一个简单的替代使actor休眠几秒钟?

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?

推荐答案

在Akka内不建议阻塞线程的任何东西。如果Actor配置了一个共享线程池(默认行为),那么使用Thread.sleep将会阻止该池中可能为其他Actors工作的线程。

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.

如果一个实际上必须阻塞,那么一个actor可以被配置为有自己的线程。这可以通过配置要使用的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的回调,例如在5秒后发送消息。

The recognized alternative to blocking is to schedule a callback to the actor via a timer, for example send a message after 5 seconds..

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

Akka调度程序在这里记录: http://doc.akka.io/docs/akka/2.3.6/scala/scheduler.html

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

这篇关于Thread.sleep里面Scala演员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆