成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Scala Actor:多線程的基礎學習

開發(fā) 后端
本文簡單介紹了Scala Actor的核心機制。Scala里多線程的基礎就是Actor,核心思想是用消息傳遞來進行線程間的信息共享和同步。

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

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

Scala Actor線程模型可以這樣理解:所有Actor共享一個線程池,總的線程個數可以配置,也可以根據CPU個數決定;當一個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實現Runnable接口,scheduler基本相當于是一個線程池,所以調用start方法之后會有一個線程來為該Actor服務。

使用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里面沒有可以處理的消息,調用suspendActor,該方法會調用wait;如果有消息,這調用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沒有可以處理的消息,設置waitingFor和continuation,這兩個變量會在接收到消息的時候使用;如果有消息,則調用scheduleActor,該方法會在線程池里選擇一個新的線程來處理,具體的處理方法也是由PartialFunction決定。不管是哪條路徑,react都會立即返回,或者說是立即拋出異常,結束該線程的執(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,就調用resumeActor,該方法會調用notify;否則就是react模型,同樣在線程池里選擇一個線程進行處理。

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

【相關閱讀】

  1. Scala入門介紹:Hello World
  2. Scala初學者學習資料:main(String[])
  3. 影響Scala語言設計的因素列表
  4. 喜歡Scala編程的四個理由
  5. Scala融合面向對象和函數概念的方法
責任編輯:yangsai 來源: Shadow & Honnix
相關推薦

2009-03-12 10:52:43

Java線程多線程

2009-10-21 15:10:27

Scala Actor

2010-07-26 13:27:19

Perl多線程

2011-06-13 10:41:17

JAVA

2009-08-14 11:35:01

Scala Actor

2010-01-15 09:15:09

Scala Actor并發(fā)

2009-06-11 10:48:53

Java多線程

2009-06-11 10:22:18

Java多線程

2009-08-26 18:13:55

C#多線程lock

2009-07-21 16:58:31

Scala變量范圍

2009-11-16 17:04:46

Inside Scal

2009-07-22 07:43:00

Scala閉包

2024-01-15 10:55:40

Python多線程開發(fā)

2010-03-17 15:45:06

Java多線程求和

2009-08-28 16:43:57

C#多線程學習

2011-08-18 17:07:23

IOS開發(fā)多線程NSInvocatio

2017-03-08 14:18:37

Linux多線程編程

2012-01-12 10:09:30

Java

2009-06-05 12:54:03

ScalaActor內存泄露

2020-04-29 11:46:16

Actor多線程CPU
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 一级片视频免费 | 亚洲国产成人精品女人久久久 | 国产精品久久久久久久久久 | av网站观看 | 人人爽人人草 | 中文字幕一区二区三区四区不卡 | 亚洲高清视频一区二区 | 九九热精品在线 | h视频在线免费 | 国内精品视频免费观看 | 天天干视频 | av在线播放一区二区 | 日韩欧美视频免费在线观看 | 毛片a区 | 久久综合久久久 | 精品久久久久一区二区国产 | 国产精品久久久久无码av | 中文字幕在线免费观看 | 色噜噜狠狠色综合中国 | 成人性生交大片免费看r链接 | 日韩欧美国产精品一区 | 久久久免费少妇高潮毛片 | 亚洲精品一二三区 | 亚洲精品久久 | 成人精品在线观看 | 伊人网站在线观看 | 国产一区二区三区高清 | 国产一区成人 | 国产美女在线观看 | 国产精品日韩欧美一区二区三区 | 成人免费一区二区三区视频网站 | 国产精品成人久久久久 | 久久黄色精品视频 | 色婷婷国产精品 | 国产ts人妖系列高潮 | 91视频免费视频 | 毛片在线看片 | 国产极品91 | 免费成人在线网站 | www.夜夜骑.com | 国产精品福利网站 |