scala 可以启动多少个 actor?
我尝试了这段代码
import scala.actors.Actor
class MyActor(val id:Int) extends Actor {
def act() {
println (" ****************** starting actor: " + id)
while (true) {
Thread.sleep(1000);
println ("I'm actor " + id)
}
}
}
object Main {
def main(args:Array[String]) {
val N = 5
for (i leftArrow 1 to N) {
val a = new MyActor(i)
println (" ++++++++++ about to start actor " + a.id)
a.start
}
println (N + " actors launched?")
}
}
并得到了这个输出
++++++++++ about to start actor 1
++++++++++ about to start actor 2
++++++++++ about to start actor 3
++++++++++ about to start actor 4
++++++++++ about to start actor 5
5 actors launched?
****************** starting actor: 1
****************** starting actor: 4
****************** starting actor: 3
****************** starting actor: 2
I'm actor 4
I'm actor 3
I'm actor 1
I'm actor 2
I'm actor 4
那么,我错过了什么,实际上只有四个演员正在启动? 这取决于我的电脑吗?一些配置?我应该开始吗 演员以不同的方式?是因为我在 netbeans 中运行该代码吗?
非常感谢 !
I tried this code
import scala.actors.Actor
class MyActor(val id:Int) extends Actor {
def act() {
println (" ****************** starting actor: " + id)
while (true) {
Thread.sleep(1000);
println ("I'm actor " + id)
}
}
}
object Main {
def main(args:Array[String]) {
val N = 5
for (i leftArrow 1 to N) {
val a = new MyActor(i)
println (" ++++++++++ about to start actor " + a.id)
a.start
}
println (N + " actors launched?")
}
}
and got this output
++++++++++ about to start actor 1
++++++++++ about to start actor 2
++++++++++ about to start actor 3
++++++++++ about to start actor 4
++++++++++ about to start actor 5
5 actors launched?
****************** starting actor: 1
****************** starting actor: 4
****************** starting actor: 3
****************** starting actor: 2
I'm actor 4
I'm actor 3
I'm actor 1
I'm actor 2
I'm actor 4
So, what I'm missing that only four actors are actually being started?
Does it depend on my computer? Some configuration? Should I start
actors in a different way? Is it because I'm running that code inside netbeans?
Thank you very much !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这与 scala 的演员池有关。它可能(我仍在等待我的书“Scala 中的 Actor”)创建一个具有四个线程的池(可能与您的四核 CPU 相关)并将您的 Actor 分配给它们。问题是,您使用了 Thread.sleep。这会吸引某个线程并绕过 scala 的 actor 进行线程分配。
I think it has to do with scala´s actor pool. It probably (I am still waiting on my book "Actors in Scala") creates a pool with four threads (perhaps related to your four core CPU) and assigns your actors to them. The problem is, that you use
Thread.sleep
. That appeals to a certain thread and bypasses scala´s actor to thread assignment.Scala 中本质上有两种 Actor(当使用标准 Scala 2.8 Actor 时):基于线程的 Actor 和基于事件的 Actor。
当您使用
receive
(或receiveWithin
)时,如您的示例所示,您正在创建基于线程的 actor,它们相对重量级。每个参与者都有一个单独的线程,只要参与者等待消息,该线程就会被阻塞。当您使用
react
而不是receive
时,您的 Actor 将是基于事件的 Actor。基于事件的 actor 更加轻量级;基于事件的参与者和线程之间不存在一对一的链接。您可以轻松创建数千个基于事件的参与者。我写了一篇博客文章(以及示例应用程序),其中包含更多详细信息。
There are essentially two kinds of actors in Scala (when using the standard Scala 2.8 actors): thread-based and event-based actors.
When you use
receive
(orreceiveWithin
), as in your example, you are creating thread-based actors, which are relatively heavyweight. There's a separate thread for each actor, which is blocked as long as the actor is waiting for a message.When you use
react
instead ofreceive
, your actor will be an event-based actor. Event-based actors are much more lightweight; there is not a one-to-one link between event-based actors and threads. You can easily create thousands of event-based actors.I've written a blog post (and example application) with more detail.