简体   繁体   English

如何在Swing中分析EDT?

[英]How do I profile the EDT in Swing?

I have an application that I'm building in Swing. 我有一个应用程序,我正在Swing中构建。 It has a scrollable and zoomable chart component which I can pan and zoom in. The whole thing is smooth except that sometimes the UI will pause for about 750 ms and I don't know why. 它有一个可滚动和可缩放的图表组件,我可以平移和放大。整个过程是顺利的,除了有时UI将暂停约750毫秒,我不知道为什么。 This doesn't always happen - but sometimes something happens in the application and it starts pausing like this once every 6-8 seconds. 这并不总是发生 - 但有时在应用程序中会发生某些事情,并且每隔6-8秒开始就像这样暂停一次。

It seems pretty clear that there's some event being placed on the EDT that's taking 750 ms or so to run, which shouldn't be happening. 很明显,EDT上有一些事件正在运行750毫秒左右,这应该不会发生。

How do I profile the EDT specifically like this? 我如何具体地描述EDT? What I would really like to do is get something that will output to a log or System.out every time an event runs on the EDT with the total amount of time the event took. 我真正想要做的是每当事件在EDT上运行时,将事件所花费的总时间量输出到日志或System.out。 Is there a way to do this? 有没有办法做到这一点?

Or is there some tool that will do this for me and give me a log of everything that runs on the EDT and how long it takes? 或者是否有一些工具可以为我做这个并给我一个在EDT上运行的所有内容的日志以及需要多长时间?

I'd like to go through this log, look at everything that's taking a long time, and find the problem. 我想通过这个日志,查看花费很长时间的所有内容,并找到问题所在。

Take a look at this question . 看看这个问题 It describes a simple log on the EDT. 它描述了EDT上的简单日志。

Create a class like this: 创建一个这样的类:

public class TimedEventQueue extends EventQueue {
    @Override
    protected void dispatchEvent(AWTEvent event) {
        long startNano = System.nanoTime();
        super.dispatchEvent(event);
        long endNano = System.nanoTime();

        if (endNano - startNano > 50000000)
            System.out.println(((endNano - startNano) / 1000000)+"ms: "+event);
    }
}

Then replace the default EventQueue with the custom class: 然后用自定义类替换默认的EventQueue:

Toolkit.getDefaultToolkit().getSystemEventQueue().push(new TimedEventQueue());

Make sure you are running only GUI related operations in EDT and there are no long running tasks in EDT. 确保在EDT中仅运行与GUI相关的操作,并且在EDT中没有长时间运行的任务。 There is one awesome tool called SwingExplorer it has one feature to monitor EDT operations. 有一个很棒的工具叫做SwingExplorer,它有一个监控EDT操作的功能。 Hope this will help. 希望这会有所帮助。

I'd say it's likely this isn't actually something on the EDT but actually garbage collection. 我想说这可能实际上并不是EDT上的东西,而是垃圾收集。

Assuming that's the case I don't know of any solution I'm afraid. 假设是这种情况我不知道任何解决方案我害怕。 I've actually never written anything in Swing which didn't have this type of behavior occasionally. 我实际上从未在Swing中写过偶尔没有这种行为的东西。

To try & debug you could subclass EventQueue & install a new one using: 要尝试和调试,您可以继承EventQueue并使用以下命令安装新的:

Toolkit.getDefaultToolkit().getSystemEventQueue().push(xxx); 。Toolkit.getDefaultToolkit()getSystemEventQueue()推(XXX)。

This should allow you to at least see what events are being processed by the EDT. 这应该允许您至少看到EDT正在处理哪些事件。

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

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