Scala中可以启动多少个演员? [英] How many actors can be launched in scala?
问题描述
我尝试过此代码
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
那么,我想念的是实际上只有四个演员在开始吗?
是否取决于我的计算机?一些配置?我应该以其他方式开始
演员吗?
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?
非常感谢!
推荐答案
我认为这与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中可以启动多少个演员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!