scala 可以启动多少个 actor?

发布于 2024-11-04 12:52:26 字数 1137 浏览 0 评论 0原文

我尝试了这段代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷到爆炸 2024-11-11 12:52:26

我认为这与 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.

情仇皆在手 2024-11-11 12:52:26

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 (or receiveWithin), 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 of receive, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文