scala进阶20-基于Actor多并发编程

时间:2022-07-03 17:59:29
import scala.actors.Actor
/**
* scala中基于actor实现并发编程,不共享数据,依赖消息传递
* pom文件中要引入scala-actors,否则找不到actors包
*/
object First_Actor extends Actor{
override def act: Unit = {
println(Thread.currentThread().getName)
for (i <- 1 to 10) {
println("Step: " + i)
Thread.sleep(2000)
}
}
}

object Second_Actor extends Actor {
override def act: Unit = {
println(Thread.currentThread().getName)
for (i <- 1 to 10) {
println("Step Further: " + i)
Thread.sleep(2000)
}
}
}

object Hello_Actor {
def main(args: Array[String]): Unit = {
First_Actor.start
Second_Actor.start
}
}