This surprises me, because that is what I included the if-clause for
that checks if the Master has as many replies (Results) as there are
Workers. Only then is it supposed to send the NewIndividuals message to
the Listener and only then should the Listener send a Finish message and
the Master will reply to that with a Finished message. Here's the new
code for Listener and Master (everything else remains the same):
class Master(nrOfWorkers:Int, nrOfMessages:Int, oldInds:ArrayBuffer[Individual], listener:ActorRef)
extends Actor {
var inds = new ArrayBuffer[Individual]
var nrOfResults:Int = _
var origin:ActorRef = _
val start:Long = System.currentTimeMillis
val workerRouter = context.actorOf(
Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)), name = "workerRouter")
def receive = {
case Broadcast =>
origin = sender
var workOn = new ArrayBuffer[Individual]
for(i <- 0 until nrOfMessages)
workOn += oldInds(i)
oldInds.trimStart(nrOfMessages)
workerRouter ! Work(workOn)
case Result(newInds) =>
inds ++= newInds
nrOfResults += 1
println("The master has " + nrOfResults + " results")
if(nrOfResults == nrOfWorkers)
listener ! NewIndividuals(inds)
case Finished =>
context.stop(self)
sender ! Finished
}
}
class Listener extends Actor {
def receive = {
case NewIndividuals(inds) =>
println("Listener coming up")
Configuration.environment(inds)
sender ! Finished
context.system.shutdown()
}
}
In
calculate() I've inserted a println before and after Await.result. Both
messages get printed before the Finished messages have been sent
("Listener coming up" has not been printed). I just don't see why the
Master apparently sends a Finished message although that should only
happen if it has received one from the Listener.