Scala并发编程【消息机制】

1.入门

 package actor

 import scala.actors.Actor
import scala.actors.migration.ActorDSL /**
* Created by zhen on 2018/11/7.
*/
class OldActor extends Actor{
def act(): Unit = {
println("scala2.10之前版本")
for(i <-1 to 2){
println("step:"+i)
Thread.sleep(1000)
}
}
}
object ActorDemo{
def main(args: Array[String]) {
//老版本Actor
val oldActor = new OldActor()
oldActor.act()
val actor = ActorDSL.actor(new Actor {
override def act(): Unit = {
println("scala2.10之后版本使用Akka机制")
for(i <-1 to 2){
println("step:"+i)
Thread.sleep(1000)
}
}
})
}
}

结果:

Scala并发编程【消息机制】

2.发送消息

 // 发送消息
val actorMessage = ActorDSL.actor(new Actor {
override def act(): Unit = {
while(true){
receive{
case data : Int => println("age is : " + data)
case _ => println("something unknown !")
}
}
}
})
actorMessage ! 18
actorMessage ! "hello everyone !"

结果:

Scala并发编程【消息机制】

上一篇:MAC 下用GCC编译报错:“Undefined symbols for architecture x86_64: ”


下一篇:windows之电脑开机出现 this product is covered by one or more of the following prtents