José Villacreses
José Villacreses

Reputation: 177

scala.actors.Actor in version 2.11.1 of the language works sequentially

I'm using version 2.11.1 of the language.

I understand, as from API documentation, that the actor system used from 2.11 onwards is AKKA. There's not even documentation for scala.actors.Actor in versions 2.11.x.

Anyway, trying out the first sample from Programming in Scala, it compiles with scala.actors.Actor on version 2.11.1 (which I find somewhat contradicting the documentation).

I have the following code:

import scala.actors.Actor

object SillyActor extends Actor {
  def act() {
    for (i <- 1 to 5) {
      println("I'm acting!")
      Thread.sleep(1000)
    }
  }
}

object SeriousActor extends Actor {
  def act() {
    for (i <- 1 to 5) {
      println("To be or not to be.")
      Thread.sleep(1000)
    }
  }
}

object ActorsMain{
  def main(args: Array[String]): Unit ={
    println(util.Properties.versionString)
    SillyActor.act()
    println("in between")
    SeriousActor.act()
  }
}

And the print out is:

version 2.11.1
I'm acting!
I'm acting!
I'm acting!
I'm acting!
I'm acting!
in between
To be or not to be.
To be or not to be.
To be or not to be.
To be or not to be.
To be or not to be.

It was supposed to be printed interleaved, not sequentially. Does it have to do with the fact that Actor is obsolete and I should be using AKKA or I'm missing something else?

Thanks for your kind help.

Upvotes: 0

Views: 189

Answers (1)

dk14
dk14

Reputation: 22374

First, scala.actors.Actor is deprecated. use akka.actor instead: http://www.scala-lang.org/api/2.11.0-M4/index.html#scala.actors.Actor. Please refer to the documentation http://doc.akka.io/docs/akka/2.3.6/scala.html

Second, you're not sending the message to the actor in your code - just calling a function on object. To send message (for Actor in deprecated library or ActorRef in akka):

actor ! msg 

Upvotes: 1

Related Questions