你们中的任何人都可以帮助我理解使用 Actor 模型在 scala 中传递的基本消息吗?
我正在尝试用 3 个演员编写一个简单的程序。
演员“BossActor”创建了 2 个演员“Actor1”和“Actor2”
“Actor1”接受 2 个整数参数 -> 找到它们的总和 -> 将总和传递给 Actor2
“Actor2”打印总和
package actorsSum
import scala.actors.Actor
import scala.actors.Actor._
case object BossActor
case object Actor1
case object Actor2
object SumOf2Numbers extends App
{
print ("Enter Num1 : ")
val Num1 = readInt()
print ("Enter Num2 : ")
val Num2 = readInt()
var Sum = 0
val bossActor = new BossActor(Num1,Num2,Sum)
bossActor.start
}
/** Boss Actor which creates and controls all subsequent actors */
class BossActor(Num1: Int, Num2: Int, Sum: Int) extends Actor {
def act() {
val act1 = new Actor1(Num1,Num2)
val act2 = new Actor2(Sum)
act1.start
act2.start
}
}
/** Actor to find sum of the numbers */
class Actor1 (Num1: Int, Num2: Int) extends Actor {
def act() {
val Grandtotal = Num1 + Num2
/** send "Grandtotal" to Actor2 */
// act2 ! Grandtotal
}
}
/** Actor to print the sum */
class Actor2(Sum: Int) extends Actor {
def act() {
react{
/** print the sum */
case Actor1 => println ("Sum: " + Sum)
case Sum => println ("Sum: " + Sum)
}
}
}