首页 > 代码库 > scala中的Actor

scala中的Actor

1.介绍

 

2.简单示例

  技术分享

 

3.第二个程序

  技术分享

 

4.通信程序

 1 package day01
 2 import scala.actors.Actor
 3 case class Message(content: String, sender: Actor)
 4 class LeoActor extends Actor{
 5   def act(){
 6     while (true){
 7       receive{
 8         case Message(content,sender)=>{
 9           println("leo: " + content)
10           sender ! "please call me after 10 minutes."
11         }
12       }
13     }
14   }
15 }
16 class JackActor(val LeoActor: Actor) extends Actor{
17   def act(){
18     LeoActor ! Message("Hello , leo, I‘m Jack. Are you free Now?",this)
19     while (true){
20       receive{
21         case re:String => println("Jack: " + re)
22       }
23     }
24   }
25 }
26 
27 object ActoyDemo {
28   def main(args: Array[String]) {
29     var Leo=new LeoActor
30     var Jack=new JackActor(Leo)
31     Leo.start()
32     Jack.start()
33   }
34 }

 

scala中的Actor