4

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 !

Ken Bloom
  • 57,498
  • 14
  • 111
  • 168
cibercitizen1
  • 20,944
  • 16
  • 72
  • 95
  • Once your question is answered, *don't* edit the question to reflect the answer. Simply accept the answer, and comment as you did below with any information that's not actually within the answer. – Ken Bloom May 03 '11 at 02:26

2 Answers2

8

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.

Peter Schmitz
  • 5,824
  • 4
  • 26
  • 48
6

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.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • If you have neither react nor receive, then what type of actor is it? It is not receiving any messages. – Jus12 May 03 '11 at 12:06
  • @Jus12 In that case, the actor system will just run all your actors, scheduling them with a thread pool mechanism. As long as the actors are running, they are taking up a thread each, so in that regard they will be like thread-based actors. The point with event-based actors is that they don't block a thread for each actor while waiting for a message (unlike thread-based actors). – Jesper May 03 '11 at 12:25