我是scala的新手,我正在尝试在scala中编写一个程序,它创建了多个(比如说30个)actor,并在它们之间传递消息。在scala中创建多个参与者
下面是我已经成功至今:
import scala.actors.Actor
import scala.util.Random
class MyActor(val id:Int, val N:Int) extends Actor {
def act() {
println ("Starting actor: " + id)
/**
react{
case str : String =>
println("Received Msg: " + str)
val randNo : Int = Random.nextInt(N)
println("Actor " + id + " Picking a random actor: " + randNo)
// Here, I should forward the message received to the ALREADY created and started actors
// val objActor = new MyActor(randNo : Int, N : Int)
// objActor.start
// objActor ! str
}
*/
}
}
object Main {
def main(args:Array[String]) {
if(args.length == 0)
{
println("Usage scala Main <numNodes>")
sys.exit()
}
val N : Int = (args(0)).toInt
// Starting all actors
for (i: Int <- 0 to N-1) {
val a = new MyActor(i : Int, N : Int)
println ("About to start actor " + a.id)
a.start
// a!"Broadcast this msg to all actors"
}
}
}
该计划的目标是创建多个角色,并从一个演员转发到另一个字符串。
上面的代码创建'N'个参数作为命令行参数。 这些actor由对象Main创建并启动。 主要应该只发送一条消息给以上创建的演员之一。 从Main接收消息的actor应该将相同的消息转发给另一个ALREADY创建/启动的actor。
这可能吗?如果是这样,你能指导我正确的方向吗?
由于提前, MS