scala-actor线程间通信

时间:2021-01-14 23:09:12
import scala.actors.Actor

case class Msg(val info: String, act1: MyActor1)

class MyActor extends Actor {
def act() {
loop {
receive {
case Msg(info, act1) =>
println("MyActor收到消息:-》" + info)
if (act1 != Nil)
act1 ! "MyActor给MyActor1回复的消息"
case _ => println("暂时没有消息")
}
}
}
} class MyActor1(mact: MyActor) extends Actor {
def act() {
mact ! Msg("MyActor1发送给MyActor的消息", this)//使用叹号这种方式发送消息
loop {
receive {
case res: String => println("MyActor1收到消息:-》" + res)
}
}
}
} var ma = new MyActor
var ma1 = new MyActor1(ma)
ma.start()
ma1.start()