How to monitor Event Dispatch Thread queue?

社会主义新天地 提交于 2019-12-02 15:51:09

问题


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).

回答1:


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.




回答2:


You need to provide more details...but thread themselves don't have queues. You can not check to see what work is waiting on a thread, you can only check the status of any given thread (say in a thread pool).

If you have a concept of "work" that needs to be queued for eventually processing by a given "worker" (ie thread) you have to implement that yourself. Alternatively you can use a an actor system such AKKA which sounds like it would do what you require



来源:https://stackoverflow.com/questions/37841987/how-to-monitor-event-dispatch-thread-queue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!