문제

I am relatively new Scala actors. I have huge map,that is grouped into smaller blocks and executed through actors. Based on map size,the number of actors created vary. The actors work well and the process is completed. But how to check the status of the generated actors? In java i am familiar with use of thread-pool executor services. In Scala how this is done?

도움이 되었습니까?

해결책

There are multiple ways to do what you want:

  1. Have the worker actor send a message back to the sender to inform it that an operation completed. Each actor has a reference to the sender actor (the one that sent the message), which you can use to send back a completion message. The sender can then handle that message.

  2. Instead of sending a message via a tell (e.g. actor ! msg), use ask, which returns a Future. You can setup a callback on the Future that runs upon completion.

  3. If the worker actors are launched for a one-time operation, have it terminate itself by stopping it once the operation finishes. The parent actor (the one which created the worker actor) can monitor the worker via a DeathWatch mechanism that informs the parent when the child actor is terminated. In this approach, termination means the operation has been completed. However, you will need to keep track of how many terminations the parent receives in order to determine when all the worker actors have finished.

Which approach to use depends on your use case and nature of the operations. Most common and flexible approach is #1. Example (not tested):

case class PerformWork(i: Int)
case object WorkDone

class ParentActor(size: Int) extends Actor {

  for (i <- 1 to size) {
    val actor = context.actorOf(Props[Worker], s"worker$i")
    actor ! PerformWork(i)
  }

  var result = 0

  def receive = {
    case WorkDone => {
      result += 1

      if (result == size) {
        // work is done
      }
    }
  }
}

class Worker extends Actor {
  def receive = {
    case m: PerformWork => {
      // do some work
      // ...

      sender ! WorkDone
    }
  }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top