iA


Increase performance by using SwingWorker

by Vijay Kiran

One of the projects I’m currently working on is a Swing application, which contains a login window and a Main Application frame.

I’ve divided the whole application into panels and the panels are pre-constructed during the menu creation. Initially, I thought it will not be a performance issue. But as the panels number grew large to 10 panels, once the login is successful, the MainFrame is taking a whooping 4000s. I profiled using Eclipse profiling tool, and found out that most of the time is spent in Menu creation. Normally it shouldn’t but as I said, I was constructing all the required panels during menu creation. So it was running inside AWT thread/UI Thread, so application frame was not visible until the construction is complete.

The panels are complete set of classes including UI, Presentation Model, Model (and Model Implementation). So that was biggest culprit and contributor for 4000s. Then I used Swing worker and pushed all the panel constructors in to the construct method. And interestingly enough, the loading time is just under 900s. Almost, 40% perfomance gain :-)

So moral of the day, always try to group the time consuming stuff and let the SwingWorker do the work !