import scala.actors._
import Actor._
case class SendRequest(rid: String)
case class Request(rid: String)
case class Response(rid: String)
val server = actor {
eventloop {
case Request(rid) =>
println("Server got request [%s] from client" format(rid))
sender ! Response(rid)
}
}
}
val client = actor {
eventloop {
case SendRequest(rid) => server ! Request(rid)
case Response(rid) =>
println("Client got response [%s] from server" format(rid))
}
}
}
用法:
scala> client ! SendRequest("Hello!")
Server got request [Hello!] from client
Client got response [Hello!] from server
至於:
所有這些通信是異步 ,因此它使用反應。
使用receive
的參與者也是異步的。他們只是阻止線程,等待一條新消息。