Scala演员-最坏的做法? [英] Scala actors - worst practices?

查看:73
本文介绍了Scala演员-最坏的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对于在Scala中使用演员有些不安全。我已经阅读了有关如何操作的文档,但我想我还需要一些不要使用的规则,以便随时使用它们。
我想我会以错误的方式使用它们,我什至不会注意到它。

I feel a bit insecure about using actors in Scala. I have read documentation about how to do stuff, but I guess I would also need some DON'T rules in order to feel free to use them. I think I am afraid that I will use them in a wrong way, and I will not even notice it.

您能想到一些如果应用的话会导致Scala演员所带来的利益破灭甚至出现错误的结果吗?

Can you think of something, that, if applied, would result in breaking the benefits that Scala actors bring, or even erroneous results?

推荐答案


  • 避免 !? 。您获得锁定的系统!

    • Avoid !? wherever possible. You will get a locked system!

      始终从Actor子系统线程发送消息。如果这意味着通过 Actor.actor 方法创建一个临时Actor,那就这样吧:

      Always send a message from an Actor-subsystem thread. If this means creating a transient Actor via the Actor.actor method then so be it:

      case ButtonClicked(src)=> Actor.actor {控制器! SaveTrade(trdFld.text)}

      其他任何消息 处理程序添加到演员的反应。否则,将无法确定您是否向错误的演员发送了消息:

      Add an "any other message" handler to your actor's reactions. Otherwise it is impossible to figure out if you are sending a message to the wrong actor:

      case other => log.warning(此+已收到意外消息 +其他

      不要使用 Actor .actor (主要演员),sublcass Actor 代替,原因是只有通过子类化,您才能提供合理的 toString 方法,如果您的日志中有如下语句,则调试actor非常困难:

      Don't use Actor.actor for your primary actors, sublcass Actor instead. The reason for this is that it is only by subclassing that you can provide a sensible toString method. Again, debugging actors is very difficult if your logs are littered with statements like:

      12:03 [INFO]将RequestTrades(2009-10-12)发送到scala.actors.Actor $ anonfun $ 1

      在系统中记录参与者,明确说明他们将收到什么消息以及他们应该如何计算响应,使用参与者会导致标准过程(通常封装在方法中)转换为分布在多个参与者反应中的逻辑。

      Document the actors in your system, explicitly stating what messages they will receive and precisely how they should calculate the response. Using actors results in the conversion of a standard procedure (normally encapsulated within a method) to become logic spread across multiple actor's reactions. It is easy to get lost without good documentation.

      始终确保您可以在其反应

      Always make sure you can communicate with your actor outside of its react loop to find its state. For example, I always declare a method to be invoked via an MBean which looks like the following code snippet. It can otherwise be very difficult to tell if your actor is running, has shut down, has a large queue of messages etc.

      def reportState = {
        val _this = this
        synchronized {
          val msg = "%s Received request to report state with %d items in mailbox".format(
                         _this, mailboxSize) 
          log.info(msg)
        }
        Actor.actor { _this ! ReportState }
      }
      




      • 将演员链接在一起并使用 trapExit = true -否则它们可能会静默失败,这意味着您的程序没有按照您的预期做,并且可能会由于消息保留在actor的邮箱中而导致内存不足。

        • Link your actors together and use trapExit = true - otherwise they can fail silently meaning your program is not doing what you think it is and will probably go out of memory as messages remain in the actor's mailbox.

          我认为围绕使用actor进行设计决策的其他一些有趣的选择已经被突出显示 此处 此处

          I think that some other interesting choices around design-decisions to be made using actors have been highlighted here and here

          这篇关于Scala演员-最坏的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆