Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many actors can be launched in scala?

Tags:

scala

actor

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 !

like image 208
cibercitizen1 Avatar asked Dec 10 '22 09:12

cibercitizen1


1 Answers

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.

like image 121
Peter Schmitz Avatar answered Dec 28 '22 22:12

Peter Schmitz