简体   繁体   English

scala:完成演员循环

[英]scala: finish actor's loop

Say I have two type of actors: Master and Slave 说我有两种类型的演员: MasterSlave

I dispatch new Jobs to Slaves, wait for their responses and process the responses. 我将新的作业分发给从属,等待其响应并处理响应。 How should I finish the Master loop once the all the slaves are finished? 一旦所有从站都完成后,我应该如何完成Master循环?

For example: 例如:

class Slave extends Actor {                                                  
  def act() {                                                                
    loop { react {                                                           
      ...                                                                    
      sender ! FinishedAll // send mesage to the master                      
      ...                                                                    
    } }                                                                      
  }                                                                          
}                                                                            

class Master extends Actor {                                                 
  loop { react {                                                           
    ...                                                                        
    case FinishedAll => exit // grrr!
    ...                                                                        
}                                                                            

I start with Scala and Actors, so the answer can be trivial :) 我从Scala和Actors开始,所以答案可能很简单:)

class Master extends Actor {
  def act() {
    var finished = false
    loopWhile( !finished ) { react {
      ...
      case FinishedAll => finished = true
      ...
    }}
  }
}

Of course, if you wait for several slaves, you need to count down the number of unfinished slaves to determine when to finish the main actor. 当然,如果您等待多个奴隶,则需要倒数未完成的奴隶的数量,以确定何时完成主要演员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM