Jakub M.
Jakub M.

Reputation: 33867

scala: finish actor's loop

Say I have two type of actors: Master and Slave

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?

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 :)

Upvotes: 2

Views: 190

Answers (1)

0__
0__

Reputation: 67330

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.

Upvotes: 5

Related Questions