SwingUtilities.invokeLater 在全速线程上 [英] SwingUtilities.invokeLater on full speed thread

查看:47
本文介绍了SwingUtilities.invokeLater 在全速线程上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a thread that does display updates on my JFrame by using SwingUtilities.invokeLater. The thread's speed is adjustable, and when it set to full speed (no sleeping between updates), my program gets slowed down badly. I guess the problem is my thread is generating too much SwingUtilities.invokeLater events that JFrame's thread can not consume. So is there anything I can do on my thread to remove previously added but not consumed events? Or should I use some other ways to update JFrame without using SwingUtilities.invokeLater?

提前致谢.

推荐答案

这对于 SwingWorker.您可以发布增量更新,SwingWorker 将批量更新以解决性能问题:

This might be a perfect job for SwingWorker. You can publish incremental updates, and SwingWorker will batch them to solve the performance problem:

因为 process 方法是在 Event Dispatch Thread 上异步调用的,所以在 process 方法执行之前可能会发生对 publish 方法的多次调用.出于性能目的,所有这些调用都合并为一个带有串联参数的调用.

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() 添加.更新列表在参数中传递给您.

The code you want to run on the EDT, you add by implementing process(). The list of updates is passed to you in the argument.

这篇关于SwingUtilities.invokeLater 在全速线程上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆