日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
ScalaActor:多線程的基礎(chǔ)學(xué)習(xí)

Scala Actor是Scala里多線程的基礎(chǔ),核心思想是用消息傳遞來進行線程間的信息共享和同步。

河曲ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!

編輯推薦:Scala編程語言專題

Scala Actor線程模型可以這樣理解:所有Actor共享一個線程池,總的線程個數(shù)可以配置,也可以根據(jù)CPU個數(shù)決定;當一個Actor啟動之后,Scala分配一個線程給它使用,如果使用receive模型,這個線程就一直為該Actor所有,如果使用react模型,Scala執(zhí)行完react方法后拋出異常,則該線程就可以被其它Actor使用。

下面看一些核心代碼。

 
 
 
  1.  def start(): Actor = synchronized {  
  2.   // Reset various flags.  
  3.   //  
  4.   // Note that we do *not* reset `trapExit`. The reason is that  
  5.   // users should be able to set the field in the constructor  
  6.   // and before `act` is called.  
  7.  
  8.   exitReason = 'normal  
  9.   exiting = false 
  10.   shouldExit = false 
  11.  
  12.   scheduler execute {  
  13.     ActorGC.newActor(Actor.this)  
  14.     (new Reaction(Actor.this)).run()  
  15.   }  
  16.  
  17.   this 
  18. }  

其中Reaction實現(xiàn)Runnable接口,scheduler基本相當于是一個線程池,所以調(diào)用start方法之后會有一個線程來為該Actor服務(wù)。

使用receive模型。

 
 
 
  1. def receive[R](f: PartialFunction[Any, R]): R = {  
  2.  assert(Actor.self == this, "receive from channel belonging to other actor")  
  3.  this.synchronized {  
  4.    if (shouldExit) exit() // links  
  5.    val qel = mailbox.extractFirst((m: Any) => f.isDefinedAt(m))  
  6.    if (null eq qel) {  
  7.      waitingFor = f.isDefinedAt  
  8.      isSuspended = true 
  9.      suspendActor()  
  10.    } else {  
  11.      received = Some(qel.msg)  
  12.      sessions = qel.session :: sessions  
  13.    }  
  14.    waitingFor = waitingForNone  
  15.    isSuspended = false 
  16.  }  
  17.  val result = f(received.get)  
  18.  sessions = sessions.tail  
  19.  result  
  20.   

如果當前mailbox里面沒有可以處理的消息,調(diào)用suspendActor,該方法會調(diào)用wait;如果有消息,這調(diào)用PartialFunction進行處理。

使用react模型。

 
 
 
  1. def react(f: PartialFunction[Any, Unit]): Nothing = {  
  2.  assert(Actor.self == this, "react on channel belonging to other actor")  
  3.  this.synchronized {  
  4.    if (shouldExit) exit() // links  
  5.    val qel = mailbox.extractFirst((m: Any) => f.isDefinedAt(m))  
  6.    if (null eq qel) {  
  7.      waitingFor = f.isDefinedAt  
  8.      continuation = f  
  9.      isDetached = true 
  10.    } else {  
  11.      sessions = List(qel.session)  
  12.      scheduleActor(f, qel.msg)  
  13.    }  
  14.    throw new SuspendActorException  
  15.  }  
  16.   

如果當前mailbox沒有可以處理的消息,設(shè)置waitingFor和continuation,這兩個變量會在接收到消息的時候使用;如果有消息,則調(diào)用scheduleActor,該方法會在線程池里選擇一個新的線程來處理,具體的處理方法也是由PartialFunction決定。不管是哪條路徑,react都會立即返回,或者說是立即拋出異常,結(jié)束該線程的執(zhí)行,這樣該線程就可以被其它Actor使用。

再來看看接收消息的處理代碼。

 
 
 
  1. def send(msg: Any, replyTo: OutputChannel[Any]) = synchronized {  
  2.  if (waitingFor(msg)) {  
  3.    received = Some(msg)  
  4.  
  5.    if (isSuspended)  
  6.      sessions = replyTo :: sessions  
  7.    else 
  8.      sessions = List(replyTo)  
  9.  
  10.    waitingFor = waitingForNone  
  11.  
  12.    if (!onTimeout.isEmpty) {  
  13.      onTimeout.get.cancel()  
  14.      onTimeout = None  
  15.    }  
  16.  
  17.    if (isSuspended)  
  18.      resumeActor()  
  19.    else // assert continuation != null  
  20.      scheduler.execute(new Reaction(this, continuation, msg))  
  21.  } else {  
  22.    mailbox.append(msg, replyTo)  
  23.  }   

如果當前沒有在等待消息或者接收到的消息不能處理,就丟到mailbox里去;相反,則進行消息的處理。這里對于receive模型和react模型就有了分支:如果isSuspended為true,表示是receive模型,并且線程在wait,就調(diào)用resumeActor,該方法會調(diào)用notify;否則就是react模型,同樣在線程池里選擇一個線程進行處理。

這樣,相信大家對Scala Actor就有了一個基本的認識。

【相關(guān)閱讀】

  1. Scala入門介紹:Hello World
  2. Scala初學(xué)者學(xué)習(xí)資料:main(String[])
  3. 影響Scala語言設(shè)計的因素列表
  4. 喜歡Scala編程的四個理由
  5. Scala融合面向?qū)ο蠛秃瘮?shù)概念的方法

分享名稱:ScalaActor:多線程的基礎(chǔ)學(xué)習(xí)
文章來源:http://www.dlmjj.cn/article/djcsoeo.html