需要刷新Java EDT(事件调度队列)

发布于 2025-01-03 19:49:45 字数 161 浏览 1 评论 0原文

我有一个 Java 应用程序,其中许多线程正在快速写入 StyledTextBox。在某个时刻,所有线程都会终止。然而,TextBox 继续接收文本一段时间,可能是因为调度队列有一点备份。是否可以以某种方式强制刷新 EDT,以便在线程终止时立即结束对 TextBox 的更新?

谢谢你, 吉姆

I have a Java app where many threads are writing to a StyledTextBox rapidly. At some point all the threads are terminated. However, the TextBox continues to receive text for a bit presumably because the dispatch queue was a bit backed up. Is it possible to somehow force the EDT to be flushed so that when the threads are terminated the updates to the TextBox end immediately?

Thank you,
Jim

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

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

发布评论

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

评论(3

心奴独伤 2025-01-10 19:49:45

You can push() your own EventQueue subclass, as shown here, and add your own flush() method to empty the queue. Note that getNextEvent() blocks if the queue is empty, so check peekEvent() first.

万劫不复 2025-01-10 19:49:45

使用队列来应用更改的文本,安排事件和 EDT,在返回之前将队列清空。一旦线程完成并且 EDT 上的另一个计划事件运行,这将导致“空”事件。

Use a queue for your changed text to apply, schedule events an the EDT that work the queue empty before returning. This would lead to "empty" events, once the threads finished and one further scheduled event on the EDT ran.

素罗衫 2025-01-10 19:49:45

好吧,我找到了解决方案。这个想法是安装一个“验证”侦听器,每当文本即将添加到 StyledText 时都会调用该侦听器。因此,当即将添加文本(从备份的调度队列)时,代码会查找要设置的标志,指示线程是否已终止。如果线程已终止,则忽略文本更新。这允许控件停止更新,同时允许事件队列被清空。下面的代码片段解决了这个问题。

txtOutputStyledText 控件。
endingThreads 是一个 boolean,当线程终止时设置为 true
这似乎是在 SWT 应用程序中处理该问题的最简单方法。

        txtOutput.addVerifyListener(new VerifyListener() {
        public void verifyText(VerifyEvent e)
        {
            if( !endingThreads )
            {
                e.doit = true;
            }
            else
            {
                e.doit = false;
            }
        }
    });

Ok I found the solution. The idea is to install a "verify" listener which gets called whenever text is about to be added to the StyledText. So when text is about to be added ( from the backed up dispatch queue ) the code looks for a flag to be set indicating whether or not the threads have been terminated. If the threads have been terminated then ignore the text update. This allows the control to stop being updated while allowing the event queue to be drained. The following code snippet solves the problem.

txtOutput is the StyledText control.
endingThreads is a boolean set to true when the threads have been terminated.
This appears to be the easiest way to handle the issue in an SWT application.

        txtOutput.addVerifyListener(new VerifyListener() {
        public void verifyText(VerifyEvent e)
        {
            if( !endingThreads )
            {
                e.doit = true;
            }
            else
            {
                e.doit = false;
            }
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文