How do I profile the EDT in Swing?

后端 未结 3 447
隐瞒了意图╮
隐瞒了意图╮ 2021-02-01 11:31

I have an application that I\'m building in Swing. It has a scrollable and zoomable chart component which I can pan and zoom in. The whole thing is smooth except that sometime

相关标签:
3条回答
  • 2021-02-01 11:46

    Take a look at this question. It describes a simple log on the EDT.

    Create a class like this:

    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 with the custom class:

    Toolkit.getDefaultToolkit().getSystemEventQueue().push(new TimedEventQueue());
    
    0 讨论(0)
  • 2021-02-01 11:48

    Make sure you are running only GUI related operations in EDT and there are no long running tasks in EDT. There is one awesome tool called SwingExplorer it has one feature to monitor EDT operations. Hope this will help.

    0 讨论(0)
  • 2021-02-01 12:00

    I'd say it's likely this isn't actually something on the EDT but actually garbage collection.

    Assuming that's the case I don't know of any solution I'm afraid. I've actually never written anything in Swing which didn't have this type of behavior occasionally.

    To try & debug you could subclass EventQueue & install a new one using:

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

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

    0 讨论(0)
提交回复
热议问题