0
votes

I have a main process that starts the process,by initiating worker actors and a master actor. Let me explain in the example

class process{

var q :collection.mutable.HashMap[] 
var map:collection.mutable.HashMap[]

var itrator = q.grouped(1000)

//start master actor
var mas:Actor=null
mas = new Master(map)
mas.start

//start worker actors
var workActor :Actor = null

    for(it<-itrator)
    {
    workActor = new Worker(map,mas)
    workActor.start
    }
}

class Worker(map:collection.mutable.HashMap[],master:Actor){

def act(){
//check for map size, break if it reaches n size
//pass the message to master to save the data
//then exit
//else
//start analysis process
//then exit
}
}

scenario 1: Once very worker actor is finished they inform the master actor,updating the count in the master actor . Once all the worker actors are finished,the MASTER actor will save the MAP.

Scenario 2: Once the size of the "map" reaches certain number,i want the worker actors to terminate and master actor shall save the data then terminate itself.

Issue: Currently after reaching n size in scenario 2,the worker actor terminates. But all the working actor terminates only after checking the n size condition. Is there way to communicate to all the currently working actors at same time to terminate after one worker reaches n size?

1

1 Answers

2
votes

If you switch over to Akka Actors instead of Scala Actors (which is recommended as Scala Actors are deprecated) then this is really simple. As long as the master actor is the supervisor/parent of the workers, then once it (the master) receives a signal from a single worker that the work is done, it stops itself thus stopping all children. The only caveat is that no worker can be stopped while in the middle of work (processing a message from the mailbox), but the stop request will stop it before it processes any more messages.