1

鉴于我Actor从内部调用一个反应,这会阻止调用Actor还是仍在处理其他请求?

class worker extends Actor()
{
  def act() = {
    loop {
      react {
        msg =>
          var foo = another_actor !? 'bar' //Block here?
          println(foo)
      }
    }
}
4

2 回答 2

3

!?总是阻塞调用线程。如果在actor react 块的中间调用,调用actor 也会被阻塞。

Actor 的主要思想是单线程执行。在当前消息完成处理之前,参与者不会继续处理下一条消息。这样,只要消息是不可变的,开发人员就不必担心并发性。

于 2009-08-10T13:56:34.863 回答
2

请参阅此问题,了解参与者无法同时处理消息(即每个参与者按顺序处理其收件箱)这一事实。

我当然不认为这从Programming in Scala中的解释中很清楚。您可以(在某种程度上)通过创建一个即时演员来实现您想要的:

loop {
   react {
      case Command(args) =>
         val f = other !! Request(args) //create a Future
         //now create inline actor
         val _this = self
         actor { //this creates an on-the-fly actor
           _this ! Result(args, f.get) //f.get is a blocking call              
         }
      case Result(args, result) =>
         //now process this 
   }
}

当然,on-the-fly actor 会阻塞,但它确实让您的原始 actor 能够处理新消息。如果所有当前池中的工作线程都忙,actor 子系统将创建新线程actors.maxPoolSize(默认为)。256

于 2009-08-10T18:15:36.717 回答