The following code is a little demonstration for Scala actors:
package myScalaProjectimport scala.actors.Actorimport scala.actors.Actor._import java.lang.Floatclass ActorTester extends Actor {def act() {println("Actor Started.")println ("The actor thread id :" + Thread.currentThread().getId())loop {receive {case i: Int => println("Int: " + i.toString)case s: String => println("String: " + s)case f: Float => println("Float: " + f)case 5=> println("Five")case _ => println("Default.")}}println("Actor ended.") //doesn’t called}}object PlayWithActors {println ("The main thread thread id :" + Thread.currentThread().getId())val myActorTester = new ActorTestermyActorTester.start//Sending data to the actormyActorTester ! "Hello actor"myActorTester ! 5myActorTester ! 164.1myActorTester ! 15}
And the Result :
The main thread thread id :1
Actor Started.
The actor thread id :12
String: Hello actor
Int: 5
Default.
Int: 15
אין תגובות:
הוסף רשומת תגובה