为什么scala actor消息队列没有界限(大小)

发布于 2024-12-06 04:51:30 字数 586 浏览 0 评论 0原文

 def append(msg: Msg, session: OutputChannel[Any]) {
    changeSize(1) // size always increases by 1
    val el = new MQueueElement(msg, session)

    if (isEmpty) first = el
    else last.next = el

    last = el
  }

MQueue(actor的消息队列)的append方法没有最大大小。这不会导致内存不足吗?

并研究一下changeSize(1)

private var _size = 0

  def size = _size
  final def isEmpty = last eq null

  protected def changeSize(diff: Int) {
    _size += diff
  }

为什么没有带有private var _size的@volatile?
如果追加时间超过 Int.maxValue 怎么办?
我们是否只是期望这些永远不会发生?

 def append(msg: Msg, session: OutputChannel[Any]) {
    changeSize(1) // size always increases by 1
    val el = new MQueueElement(msg, session)

    if (isEmpty) first = el
    else last.next = el

    last = el
  }

the append method of the MQueue(message queue of the actor) have no maximum size. Won't this cause outOfMemory?

And look into the changeSize(1)

private var _size = 0

  def size = _size
  final def isEmpty = last eq null

  protected def changeSize(diff: Int) {
    _size += diff
  }

why no @volatile with private var _size ?
what if append times exceed the Int.maxValue?
do we just expect those will never happend?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的海 2024-12-13 04:51:30

对于问题的第一部分:是的,另请参阅此相关问题演员邮箱溢出。 Scala

我认为 _size 变量没有标记为易失性,因为这里期望调用方法负责同步。我简单地浏览了代码,调用此方法的 actor 库的各个部分确实被标记为同步。
对于整数溢出:我想确实预计这种情况永远不会发生。
最常用的 actor 库是 Akka,它确实支持有界邮箱,请参阅 此链接了解语义和配置。除此之外,它们将替换/与 scala 发行版中的 actor 库合并: 正如最近讨论的邮件列表

For the first part of your question: yes, see also this related question Actors Mailbox Overflow. Scala

I think the _size variable is not marked as volatile because it is expected here that the calling method take care of the synchronization. I briefly scanned through the code and various parts of the actor library that call this method are indeed marked as synchronized.
For the integer overflow: I guess it is indeed expected that this will never happen.
The actor library that is most used is Akka, which does have support for bounded mailboxes, see this link for semantics and configuration. Apart from that they will replace/be merged with the actors library in the scala distribution: as discussed recently on the mailing list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文