如何合并或跳过 Scala Actor 中的重复消息?

发布于 2024-11-04 08:38:44 字数 242 浏览 0 评论 0原文

假设您有一个 gui 组件,并且 10 个线程都告诉它在足够长的时间内重新绘制,因为它们都在单个绘制操作发生之前到达。不要天真地浪费资源重绘 10 次,只需合并/忽略除最后一个之外的所有内容并重绘一次(或更可能是两次 - 第一次为第一次,最后一次为最后一次)。我的理解是 Swing 重绘管理器执行此操作。

有没有办法在 Scala Actor 中完成同样类型的行为?有没有办法查看队列并合并消息,或者忽略某种类型或其他类型的最后一个消息之外的所有消息?

Let's say you have a gui component and 10 threads all tell it to repaint at sufficiently the same time as they all arrive before a single paint operation takes place. Instead of naively wasting resources repainting 10 times, just merge/ignore all but the last one and repaint once (or more likely, twice--once for the first, and once for the last). My understanding is that the Swing repaint manager does this.

Is there a way to accomplish this same type of behavior in a Scala Actor? Is there a way to look at the queue and merge messages, or ignore all but the last of a certain type or something?

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

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

发布评论

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

评论(1

疧_╮線 2024-11-11 08:38:46

像这样的东西?:

act = 
  loop { 
    react {
      case Repaint(a, b) => if (lastRepaint + minInterval < System.currentTimeMillis) {
          lastRepaint = System.currentTimeMillis
          repaint(a, b)
    }
  }

如果你想在演员的线程有机会时重新绘制,但不再这样,那么:
(更新:使用最后的消息参数重新绘制)

act =
  loop {
    react {
      case r@Repaint(_, _) =>  
        var lastMsg = r
        def findLast: Unit = {
          reactWithin(0) {
            case r@Repaint(_, _) => 
              lastMsg = r
            case TIMEOUT => repaint(lastMsg.a, lastMsg.b)
          }
        }
        findLast
    }
  }

Something like this?:

act = 
  loop { 
    react {
      case Repaint(a, b) => if (lastRepaint + minInterval < System.currentTimeMillis) {
          lastRepaint = System.currentTimeMillis
          repaint(a, b)
    }
  }

If you want to repaint whenever the actor's thread gets a chance, but no more, then:
(UPDATE: repainting using the last message arguments)

act =
  loop {
    react {
      case r@Repaint(_, _) =>  
        var lastMsg = r
        def findLast: Unit = {
          reactWithin(0) {
            case r@Repaint(_, _) => 
              lastMsg = r
            case TIMEOUT => repaint(lastMsg.a, lastMsg.b)
          }
        }
        findLast
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文