丢弃除Scala actor中的最后一条消息以外的所有消息 [英] Discard all messages except the last one in a Scala actor

查看:93
本文介绍了丢弃除Scala actor中的最后一条消息以外的所有消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SwingWorker actor,它从它发送的参数对象中计算出要显示的绘图;然后在EDT线程上绘制图。一些GUI元素可以调整此图的参数。当它们更改时,我生成一个新的参数对象并将其发送给工作程序。

I have a SwingWorker actor which computes a plot for display from a parameters object it gets send; then draws the plot on the EDT thread. Some GUI elements can tweak parameters for this plot. When they change I generate a new parameter object and send it to the worker.

到目前为止,该方法有效。

This works so far.

现在,在移动滑块时,会创建许多事件并将其排队在工作人员的邮箱中。但是我只需要为最后一组参数计算图。有没有办法删除收件箱中的所有邮件?保留最后一个并只处理最后一个?

Now when moving a slider many events are created and queue up in the worker's mailbox. But I only need to compute the plot for the very last set of parameters. Is there a way to drop all messages from the inbox; keep the last one and process only that?

val worker = new SwingWorker {
  def act() {
    while (true) {
      receive {
        case params: ExperimentParameters => {
          //somehow expensive
          val result = RunExperiments.generateExperimentData(params)

          Swing.onEDT{ GuiElement.redrawWith(result) }
        }
      }
    }
  }
}


推荐答案

同时我找到了解决方案。您可以检查角色的邮箱大小,如果不为0,则跳过该消息。

Meanwhile I have found a solution. You can check the mailbox size of the actor and simply skip the message if it is not 0.

val worker = new SwingWorker {
  def act() {
    while (true) {
      receive {
        case params: ExperimentParameters => {
          if( mailboxSize == 0) {
            //somehow expensive
            val result = RunExperiments.generateExperimentData(params)
            Swing.onEDT{ GuiElement.redrawWith(result) }
          }
        }
      }
    }
  }
}

这篇关于丢弃除Scala actor中的最后一条消息以外的所有消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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