Scala Actor:多线程的基础学习

开发 后端
本文简单介绍了Scala Actor的核心机制。Scala里多线程的基础就是Actor,核心思想是用消息传递来进行线程间的信息共享和同步。

Scala Actor是Scala里多线程的基础,核心思想是用消息传递来进行线程间的信息共享和同步。

51CTO编辑推荐:Scala编程语言专题

Scala Actor线程模型可以这样理解:所有Actor共享一个线程池,总的线程个数可以配置,也可以根据CPU个数决定;当一个Actor启动之后,Scala分配一个线程给它使用,如果使用receive模型,这个线程就一直为该Actor所有,如果使用react模型,Scala执行完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都会立即返回,或者说是立即抛出异常,结束该线程的执行,这样该线程就可以被其它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

2011-06-13 10:41:17

JAVA

2010-07-26 13:27:19

Perl多线程

2010-01-15 09:15:09

Scala Actor并发

2009-06-11 10:22:18

Java多线程

2009-06-11 10:48:53

Java多线程

2009-08-14 11:35:01

Scala Actor

2009-08-26 18:13:55

C#多线程lock

2024-01-15 10:55:40

Python多线程开发

2011-08-18 17:07:23

IOS开发多线程NSInvocatio

2010-03-17 15:45:06

Java多线程求和

2009-08-28 16:43:57

C#多线程学习

2017-03-08 14:18:37

Linux多线程编程

2020-04-29 11:46:16

Actor多线程CPU

2009-06-05 12:54:03

ScalaActor内存泄露

2012-01-12 10:09:30

Java

2009-11-16 17:04:46

Inside Scal

2009-07-21 16:58:31

Scala变量范围

2009-07-22 07:43:00

Scala闭包
点赞
收藏

51CTO技术栈公众号