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)
}
}
})
}
}
结果:
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 !"
结果: