How to profile EDT in Swing?

I have an application that I create in Swing. It has a scrollable and scalable chart component that I can pan and zoom. All this is smooth, except that sometimes the user interface stops for about 750 ms, and I don’t know why. This does not always happen, but sometimes something happens in the application, and it starts as a pause every 6-8 seconds.

It seems pretty obvious that there is some kind of event in the EDT that fires 750 ms or so, which shouldn't happen.

How do I configure EDT this way? I would really like to get what will be logged or System.out every time the event is fired on EDT with the total amount of time that has occurred. Is there any way to do this?

Or is there any tool that will do this for me and give me a log of everything that works on EDT and how long it will take?

I would like to go through this magazine, look at everything that takes a lot of time, and find the problem.

+4
source share
3 answers

Take a look at the question . He describes a simple magazine on EDT.

Create the class as follows:

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

Then replace the default EventQueue event with a custom class:

 Toolkit.getDefaultToolkit().getSystemEventQueue().push(new TimedEventQueue()); 
+8
source

Make sure that only operations with the graphical interface are performed in the EDT and that there are no lengthy tasks in the EDT. There is one amazing SwingExplorer tool, it has one function for controlling EDT operations. Hope this helps.

0
source

I would say that this is more likely not something on EDT, but actually garbage collection.

Assuming I don't know a single solution, I'm afraid. I never wrote anything in Swing, which did not have this behavior sometimes.

To try and debug, you can subclass EventQueue and set a new one using:

Toolkit.getDefaultToolkit () getSystemEventQueue () click (xxx); ..

This should allow you to at least see which events are being processed by the EDT.

0
source

Source: https://habr.com/ru/post/1346592/


All Articles