如何监控事件指派线程队列? [英] How to monitor Event Dispatch Thread queue?

查看:358
本文介绍了如何监控事件指派线程队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个沉重的java项目不响​​应用户的工作。我发现,在事件调度线程长时间需要的事件会导致我的项目工作缓慢。所以,我有两个问题,以这种方式:


  1. 如何监控事件调度线程,看看哪些事件是在排队,最终,哪些花更多的时间在事件调度线程? (正如你所知道的invokeLater功能插入事件到事件派发队列的末尾,我要监控整个事件调度队列)

  2. 如何插入一个事件来第一个事件调度线程的? (在这种情况下,图形用户界面将响应)。


解决方案

通常情况下,缓慢由code无意中尝试从UI线程访问文件系统,网络或数据库造成的。它通常是pretty容易SwingWorkers修复,一旦原因已查明。

更换事件调度队列与的实施监视队列中的长事件是如何采取的过程。

当一个事件的时间太长了新的队列执行记录的EDT的堆栈跟踪。结果
如果你只是用来在踪迹看时抛出异常不要惊慌。新的做法是杀得慢的事件或导致他们抛出异常,它只是显示你什么EDT在瞬间被检测到时,反应迟钝做。
看印刷踪迹,并找出如何事件的部分缓慢移动到另一个线程。

我相信Netbean的缓慢探测器实现了非常类似的东西。

I have a heavy java project which does not work responsive to the users. I have found out that long time needed events in event dispatch thread can cause my project to work slowly. So, I have two question in this manner:

  1. How can I monitor the event dispatch thread and see which events are in the queue and eventually, which ones spend more time in event dispatch thread? ( As you know invokeLater function insert the event to the end of the event dispatch queue. I want to monitor whole event dispatch queue)
  2. How can I insert an event to the first of the event dispatch thread? (in this case, the GUI will be responsive).

解决方案

Typically the slowness is caused by code that inadvertently tries to access the filesystem, network or database from the UI thread. Its usually pretty easy to fix with SwingWorkers once the cause has been identified.

Replace the Event Dispatch Queue with an implementation that monitors how long events in the queue take to process.

When an event takes too long the new queue implementation logs the stacktrace of the EDT.
If you are only used to looking at stacktraces when exceptions are thrown don't get alarmed. The new implementation isn't killing slow events or causing them to throw exceptions its just showing you what the EDT is doing at the moment when the unresponsiveness was detected. Look at the printed stacktraces and figure out how to move the slow part of the event into another thread.

I believe Netbean's slowness detector implements something very similar.

这篇关于如何监控事件指派线程队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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