Java JTable 更新频繁?
我正在编写一个能够处理频繁更新的 JTable。通常,此 JTable 有约 1000 行数据,并且某些列会频繁更新,因为它们的值源自股票价格,而股票价格在市场交易时间内波动很大。
我面临的问题是,当更新大量行(例如 80%)时,JTable 会变得非常慢,持续 20-30 秒左右,探查器显示 EDT 线程非常繁忙,正在处理 tableChanged 调用。
我尝试通过以下方式折叠更改 1)抑制fireTableCellUpdated 2) 如果有 <= 50 行更改,则对各个行调用 fireTableRowUpdate 3) 如果有> 50 行更改,对整个表调用 fireTableDataChanged。
更好,但是当频繁更新时仍然很慢,我的理解是 fireTableDataChanged 也很慢。因此,如果数据更新足够频繁,fireTableDataChanged 就会被频繁调用,GUI 就会感觉迟缓。
有此主题经验的人可以推荐使用 fireTableRowsUpdate 的最佳实践吗? fireTableDataChanged 和 fireTableStructureChanged 可以提高 GUI 的活跃度和性能吗?如果您有指向解决此问题的示例代码的指针,那就更好了。
非常感谢
安东尼斯
I'm coding a JTable that is capable of handling frequent updates. Typically this JTable has ~1000 rows of data, and some of the columns will update frequently because their values are derived from stock price which moves a lot during market hours.
The problem I'm facing is that when large # of rows (e.g. 80%) are updating, the JTable become very slow for 20-30 seconds or so, which the profiler shows that the EDT thread is extremely busy, handling tableChanged calls.
I try to collapse the changes by
1) suppressing fireTableCellUpdated
2) If there are <= 50 rows changes, call fireTableRowUpdate on individual rows
3) If there > 50 rows of changes, calling fireTableDataChanged for the entire table.
It's better, but still slow when there are frequent updates, my understanding is that fireTableDataChanged is slow as well. So if data updates frequent enough, fireTableDataChanged will be called frequently, and the GUI will feel sluggish.
Can someone experienced in this subject recommend a best practice in using fireTableRowsUpdate,
fireTableDataChanged and fireTableStructureChanged to improve GUI liveliness and performance? If you have pointers to sample codes that address this problem it'll be even better.
Many thanks
Anthony Si
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经这样做了,事实上,即使在强大的设置上,默认的 JTable 性能也非常糟糕。但我们并没有失去所有希望:使用(相当)一些技巧,您可以获得可接受的性能。
基本上,Oracle 有一个专门用于此目的的教程,称为“圣诞树”。
在这里,“如何创建性能良好的经常更新的 JTable”:
http: //www.oracle.com/technetwork/java/christmastree-138396.html
我发现真正令人惊奇的一件事是不断显示所使用的内存:您可能想这样做。
您会惊讶地发现默认 JTable 生成了多少不必要的垃圾,减慢了一切,当然也使 GC 触发的频率比应有的要高。
然后开始实施我给您的链接中给出的所有技巧,您会发现一切都会运行得更加更加顺利。我现在正在运行非常复杂且“不断更新”的 JTable,现在一切都很好:)
但是,是的,除了最简单的情况和少量数据之外,默认的 JTable 实现确实非常糟糕。
I've done that and indeed, even on beefy setups the default JTable perfs are terribly bad. But all hope is not lost: using (quite) a few tricks you can get acceptable performances.
Basically, Oracle has a tutorial precisely for this purpose called the "Christmas tree".
Here you go, "How to create frequently updated JTable that perform well":
http://www.oracle.com/technetwork/java/christmastree-138396.html
One of the thing that I found really amazing was to constantly display the memory used: you may want to do that.
You'll be amazed at how much needless crap is generated by a default JTable, slowing everything down and making of course the GC trigger way more often than it should.
Then start implementing all the tricks given in the link I gave you and you'll see that everything shall run much smoother. I'm now running very complex and "constantly updated" JTable and all is fine now :)
But yup, besides for the simplest case and tiny amounts of data, the default JTable implementation is really terribly bad.