Scala实战高手****第17课:Scala并发编程实战及Spark源码阅读
package com.wanji.scala.test import javax.swing.text.AbstractDocument.Content import scala.actors.Actor case class Hello(name:String,content:String,send:Actor) case class HelloBack(name:String,content: String,sender:Actor) /** * 描述:Scala编程实战 * 作者: sunrunzhi * 时间: 2018/11/22 09:23 */ class HelloActor extends Actor{ var counter=0 override def act(): Unit ={ while(true) { receive{ case content:String=>println(content) case Hello(name,content,sender)=>{ println("HelloActor:"+name+" content:"+content+counter) counter+=1 Thread.sleep(3000) sender ! HelloBack(name,content,this) } } } } } class HelloBackActor(val helloActor: Actor) extends Actor{ var counter=0 override def act(): Unit = { helloActor ! Hello("Spark","Love is here",this) while (true){ receive{ case content:String=>println("Message:"+content) case HelloBack(name,content,sender)=>{ println("HelloBackActor:"+name+" content:"+content+counter) counter+=1 Thread.sleep(3000) sender ! Hello(name,content,this) } } } } } object C17 { def main(args: Array[String]): Unit = { val helloActor=new HelloActor helloActor.start()//启动start来接收消息 val helloBackActor=new HelloBackActor(helloActor) helloBackActor.start() var counter=0 while(true){ helloActor ! "Life is short,you need Spark!!!"+counter counter +=1 Thread.sleep(3000) } } }
package com.wanji.scala.test
import javax.swing.text.AbstractDocument.Content
import scala.actors.Actor
case class Hello(name:String,content:String,send:Actor)
case class HelloBack(name:String,content: String,sender:Actor)
/**
* 描述:Scala编程实战
* 作者: sunrunzhi
* 时间: 2018/11/22 09:23
*/
class HelloActor extends Actor{
var counter=0
override def act(): Unit ={
while(true) {
receive{
case content:String=>println(content)
case Hello(name,content,sender)=>{
println("HelloActor:"+name+" content:"+content+counter)
counter+=1
Thread.sleep(3000)
sender ! HelloBack(name,content,this)
}
}
}
}
}
class HelloBackActor(val helloActor: Actor) extends Actor{
var counter=0
override def act(): Unit = {
helloActor ! Hello("Spark","Love is here",this)
while (true){
receive{
case content:String=>println("Message:"+content)
case HelloBack(name,content,sender)=>{
println("HelloBackActor:"+name+" content:"+content+counter)
counter+=1
Thread.sleep(3000)
sender ! Hello(name,content,this)
}
}
}
}
}
object C17 {
def main(args: Array[String]): Unit = {
val helloActor=new HelloActor
helloActor.start()//启动start来接收消息
val helloBackActor=new HelloBackActor(helloActor)
helloBackActor.start()
var counter=0
while(true){
helloActor ! "Life is short,you need Spark!!!"+counter
counter +=1
Thread.sleep(3000)
}
}
}