java - 全速线程上的 SwingUtilities.invokeLater

标签 java multithreading swing

我有一个线程使用 SwingUtilities.invokeLater 在我的 JFrame 上显示更新。线程的速度是可调的,当它设置为全速(更新之间没有 sleep )时,我的程序会严重减慢。我想问题是我的线程生成了太多 JFrame 线程无法使用的 SwingUtilities.invokeLater 事件。那么我可以在我的线程上做些什么来删除以前添加但未消耗的事件吗?或者我应该使用其他一些方法来更新 JFrame 而不使用 SwingUtilities.invokeLater?

提前致谢。

最佳答案

这可能是 SwingWorker 的完美工作。您可以发布增量更新,SwingWorker 将批量更新它们以解决性能问题:

Because the process method is invoked asynchronously on the Event Dispatch Thread multiple invocations to the publish method might occur before the process method is executed. For performance purposes all these invocations are coalesced into one invocation with concatenated arguments.

您想要在 EDT 上运行的代码,可以通过实现 process() 添加。更新列表在参数中传递给您。

关于java - 全速线程上的 SwingUtilities.invokeLater,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13114097/

相关文章:

java - 解码意外元素错误

java - 最佳编程环境

c# - 两个并发的 NetworkStream.BeginWrite 调用会发生什么?

java - TableModelListener 并不总是工作

java - 设置 JFileChooser 的最小/最大大小 - Java

java - 哪种布局和容器适合制作像 Tiled 这样的 GUI?

java - 扩展 Swing 的 UndoManager 以提供重复和多个撤消/重做

JavaFX : how to design a chess table with event handler on each square

java - 在 Android 应用程序中非 Activity 状态下等待之前,对象未被线程锁定

Java 8 使用 boolean 值打开并行()流?