需要刷新Java EDT(事件调度队列)
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以
push()
您自己的EventQueue
子类,如此处,并添加您自己的flush()
方法来清空队列。请注意,如果队列为空,getNextEvent()
会阻塞,因此请先检查peekEvent()
。You can
push()
your ownEventQueue
subclass, as shown here, and add your ownflush()
method to empty the queue. Note thatgetNextEvent()
blocks if the queue is empty, so checkpeekEvent()
first.使用队列来应用更改的文本,安排事件和 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.
好吧,我找到了解决方案。这个想法是安装一个“验证”侦听器,每当文本即将添加到
StyledText
时都会调用该侦听器。因此,当即将添加文本(从备份的调度队列)时,代码会查找要设置的标志,指示线程是否已终止。如果线程已终止,则忽略文本更新。这允许控件停止更新,同时允许事件队列被清空。下面的代码片段解决了这个问题。txtOutput
是StyledText
控件。endingThreads
是一个boolean
,当线程终止时设置为true
。这似乎是在 SWT 应用程序中处理该问题的最简单方法。
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 theStyledText
control.endingThreads
is aboolean
set totrue
when the threads have been terminated.This appears to be the easiest way to handle the issue in an SWT application.