1. 程式人生 > >Spark基礎-Scala中actor的操作

Spark基礎-Scala中actor的操作

核心
1、scala中actor的操作

package com.xlucas
import sun.plugin2.jvm.RemoteJVMLauncher.CallBack

import scala.actors._
/**
  * Created by xlucas on 2017/8/27 0027.
  */
object ActorOop {
  def main(args: Array[String]): Unit = {

    val callback =new callBack()  //例項化 callback
    callback.start()//啟動執行緒
    val call= new
call(callback) //例項化 callback call.start()//啟動執行緒 } } case class hello(name:String,context:String,sender:Actor) case class helloback(name:String,context:String,sender:Actor) class call(val callBack: Actor) extends Actor{ var count=0 def act(): Unit ={ //重寫方法 callBack!helloback("spark","hadoop "
,this) //傳送訊息 while (true){ receive{ case hello(name,context,sender)=>{ println("hello"+" name: "+name+" context:"+context+" count: "+count) count +=1 sender!helloback(name,context,this)//傳送訊息 Thread.sleep(3000) } } } } } class
callBack extends Actor {
var count=0 def act(): Unit = ( while(true){ receive{ case helloback(name,context,sender)=>{ println("helloBack"+" name: "+name+" context:"+context+" count: "+count) count +=1 sender!hello(name,context,this) //傳送訊息 Thread.sleep(3000) } } } ) }

執行結果
helloBack name: spark context:hadoop count: 0
hello name: spark context:hadoop count: 0
helloBack name: spark context:hadoop count: 1
hello name: spark context:hadoop count: 1
helloBack name: spark context:hadoop count: 2
hello name: spark context:hadoop count: 2