java中如何检测主线程何时冻结GUI?
我想检测主线程中的一些耗时操作何时导致 gui 冻结。 我的目标是自动设置和取消设置等待光标。
谢谢
I want to detect when some time consumption operations in main thread cause gui freeze.
My target is to set and unset wait cursor automatically.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你是本末倒置:你的主线程首先不应该执行任何耗时的操作 - 它们应该始终在单独的线程中具体化,以便你的 GUI 可以保持响应(例如显示状态)操作,或提供中止操作的可能性)。
I think you're putting the cart before the horse: Your main thread shouldn't do any time-consuming operations in the first place - they should always be externalized in separate threads, so that your GUI can stay responsive (and e.g. show status on the operations, or provide the possibility to abort them).
我认为这可能会有所帮助: http://www.javaspecialists.eu/archive/Issue075.html< /a> 和 http://www.javaworld.com/javaworld/javatips/jw-javatip87.html。
I think this could be helpful: http://www.javaspecialists.eu/archive/Issue075.html and http://www.javaworld.com/javaworld/javatips/jw-javatip87.html.
您可以让一个线程轮询 GUI 线程的堆栈跟踪以确定它是空闲还是忙碌。如果过于频繁,您可以将其正在执行的操作(堆栈跟踪)记录到日志中。最初,记录每个非空闲堆栈跟踪并找出哪些不值得记录可能会很有趣。
You can have a thread which polls the GUI thread's stack trace to determine whether it is idle or busy. If busy too often, you can log what it is doing (the stack trace) to a log. Initially it might be interesting to record every non-idle stack trace and work out which ones are not worth logging.
此 EDT 锁定检测代码将通过添加看门狗来完成这项工作。
EventQueueWithWD.java
:SampleEQUsage.java
:This EDT lockup detection code will do the job by adding watch dogs.
EventQueueWithWD.java
:SampleEQUsage.java
: