简体   繁体   English

EventQueue工作缓慢

[英]Slow work of EventQueue

I have a simple JAVA program with gui that just increments int variable and displays its value in JLabel. 我有一个带有gui的简单JAVA程序,它只会递增int变量并在JLabel中显示其值。 I create new thread for proper(thread-safe) updating JLabel by calling inside it EventQueue.invokeLater() with Runnable class which run method simply does 我通过使用Runnable类在其内部调用EventQueue.invokeLater()来创建用于适当(线程安全)更新JLabel的新线程,而runnable类只是运行方法

EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            label.setText("" + number);
        }
    });

When i run program, as expected label's number starts to grow rapidly from 1 to about 5000 but then it starts to slow down and i'm starting to see such label's updates like 100255, 173735, 235678 and big pauses between them with blocked GUI. 当我运行程序时,正如预期的那样,标签的数量开始从1迅速增加到大约5000,但随后开始放缓,并且我开始看到此类标签​​的更新,例如100255、173735、235678以及它们之间的大停顿以及被阻止的GUI。 But when i compile without using EventQueue.invokeLater(), just calling directly label.setText("" + number); 但是,当我不使用EventQueue.invokeLater()进行编译时,只需直接调用label.setText("" + number); everything works fine and perfect and i can see how each number of my label is changing extremely fast. 一切正常且完美,我可以看到标签上每个数字的变化都非常快。 But of course i realize in that case my method isn't thread-safe. 但是我当然知道在那种情况下我的方法不是线程安全的。

What's the problem? 有什么问题? It seems to me that EventQueue works slow or something. 在我看来,EventQueue的工作速度很慢。

Probably, the event queue is being choked up. 可能是事件队列被阻塞了。 You may want to look at coalescing the events to remove redundant entries when you are queuing event faster than they can be dequeued and actioned. 您可能希望查看合并事件以在对事件进行排队时删除多余的条目,其速度要比对它们进行出队和处理的速度快。

Every time an event is added to the queue, the existing events are queried to see if they merge the new event with themselves. 每次将事件添加到队列时,都会查询现有事件以查看它们是否将新事件与自身合并。 As the queue backs up, more and more events have to be so queried, and the system gets progressively further behind. 随着队列的备份,必须如此查询越来越多的事件,并且系统逐渐落后。 This is useful for mouse events, but in a simple (and artificial) case like this it can be detrimental. 这对于鼠标事件很有用,但是在这种简单(人为)的情况下,这可能是有害的。

Having said that, I vaguely recall that the GUI code is optimized to not attempt coalescing events that don't override the appropriate method, so your problem may be just a simple backlog. 话虽如此,我隐约记得,GUI代码经过优化,不会尝试合并不会覆盖适当方法的事件,因此您的问题可能只是一个简单的待办事项。

Instead of calling setText directly, you could create a custom event for setting text on a component, implement coalescing for it and use that instead so that at any given time only the most recent text is pending. 您可以创建一个用于在组件上设置文本的自定义事件,而不是直接调用setText ,对其实现合并并使用它,以便在任何给定时间仅等待最新的文本。 If you do this and you want to set the text based on what was previously set it's better to retain the value and always set the GUI widget from that rather than recalling the GUI widget's current value with getText . 如果执行此操作,并且要基于先前设置的内容设置文本,则最好保留该值,并始终从中设置GUI小部件,而不是使用getText调用GUI小部件的当前值。 Otherwise merging is much more difficult. 否则,合并要困难得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM