如何在 Swing 中分析 EDT? [英] How do I profile the EDT in Swing?

查看:58
本文介绍了如何在 Swing 中分析 EDT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 Swing 中构建的应用程序.它有一个可滚动和可缩放的图表组件,我可以平移和放大.整个过程很流畅,只是有时 UI 会暂停大约 750 毫秒,我不知道为什么.这并不总是发生 - 但有时应用程序中会发生某些事情,并且每隔 6-8 秒就会开始像这样暂停一次.

I have an application that I'm building in 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. This doesn't always happen - but sometimes something happens in the application and it starts pausing like this once every 6-8 seconds.

很明显,在 EDT 上放置了一些需要 750 毫秒左右运行的事件,这不应该发生.

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?我真正想要做的是每次事件在 EDT 上运行时都会输出到日志或 System.out 以及事件花费的总时间.有没有办法做到这一点?

How do I profile the EDT specifically like this? 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. Is there a way to do this?

或者是否有一些工具可以为我执行此操作,并为我提供在 EDT 上运行的所有内容以及需要多长时间的日志?

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?

我想通过这个日志,查看所有需要很长时间的东西,然后找出问题所在.

I'd like to go through this log, look at everything that's taking a long time, and find the problem.

推荐答案

看看这个 问题.它描述了 EDT 上的一个简单日志.

Take a look at this question. It describes a simple log on the EDT.

创建一个这样的类:

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);
    }
}

然后用自定义类替换默认的EventQueue:

Then replace the default EventQueue with the custom class:

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

这篇关于如何在 Swing 中分析 EDT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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