测量“忙碌"事件调度线程 [英] Measuring "busyness" of the event dispatching thread
问题描述
我想衡量我的事件调度线程的繁忙程度".一种可能的想法是设置一个后台线程,执行以下操作:
I would like to measure the "busyness" of my Event Dispatching Thread. One possible idea is to set up a background thread that does something like:
while(true) {
final long[] end = new long[1]; // Array to get stuff out from Runnable.
long start = System.nanoTime();
EventQueue.invokeAndWait(new Runnable() {
public void run() {
end[0] = System.nanoTime();
}
});
long queueTimeNs = end[0] - start;
// Report the queue time somewhere.
Thread.sleep(100); // Poll the EDT < 10 times/s.
}
这个想法是测量从将事件发送到 EDT 到它被分派所需的时间.这将粗略地了解用户界面的响应能力.
The idea is to measure how long it takes from sending an event to the EDT to it getting dispatched. This would give a rough idea about the responsiveness of the UI.
这有意义吗?有没有更标准的方法来做类似的事情?
Does this make any sense? Is there some more standard way to do something similar?
推荐答案
为此我使用了一个出色的工具:SwingExplorer.它允许您检查 Swing 组件、查看它们的绘制方式、检测 EDT 违规以及检测 EDT 的挂起.基本上,您输入一个以毫秒为单位的持续时间值,然后您就可以使用您的应用程序.当 EDT 挂起的时间超过此持续时间时,挂起会记录在工具的 UI 中.
I use an excellent tool for this : SwingExplorer. It allows you to inspect your Swing components, to see how they are drawn, to detect the EDT violations, and to detect the hangs of the EDT. Basically, you enter a duration value in milliseconds, and then you play with your app. When the EDT hangs for more than this duration, the hang is logged in the tool's UI.
官方网站是https://swingexplorer.dev.java.net,但似乎在我写这个答案的那一刻下来.你可以找到Eclipse和NetBeans的插件,你也可以在maven仓库上找到swingexplorer,如果你使用maven(抱歉,我暂时找不到链接)
The official site is https://swingexplorer.dev.java.net, but it seems to be down at the moment I write this answer. You can find a plugin for Eclipse and NetBeans, and you can also find swingexplorer on a maven repository, if you use maven (sorry, I can't find a link for the moment)
至少存储库仍然可用:cvs -d :pserver:guest:guest@cvs.dev.java.net:/shared/data/ccvs/repository co swingexplorer
我查看了 Swing explorer 的源代码,似乎他们编写了一个自定义的 EventQueue
来检查 EDT 行为.该代码似乎与另一个项目有关,SwingHelper.
I looked at the source code of Swing explorer, and it appears that they wrote a custom EventQueue
to inspect the EDT behavior. The code seems to be related to another project, SwingHelper.
该项目的网站将很快返回 http://java.net/projects/swingexplorer
The project's site will come back soon at http://java.net/projects/swingexplorer
这篇关于测量“忙碌"事件调度线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!