The Source for Java Technology Collaboration
User: Password:



John O'Conner

John O'Conner's Blog

Starting off on the right thread

Posted by joconner on January 11, 2007 at 07:27 PM | Comments (5)

A new year, new beginnings. Times like this always make me resolve impossible things. Fortunately, there's one thing I can follow through with. I can certainly start off on the right foot, and in this particular resolution, that means starting off on the right thread too.

While working on a new article for Sun's Java Developer site, I realized that I had been making a mistake over and over again. I've been starting app UIs from the wrong thread. In my situation, I haven't noticed anything horribly wrong, but I suspect that problems are possible...strange synchronization problems that are hard to duplicate reliably.

If you're developing Swing applications, you have an important rule to remember. With very few exceptions, you can interact with GUI components and their models only from the Swing event dispatch thread. However, I see apps start up like this all the time:

public class MyApplication extends JFrame {
  ...
  public static void main(String[] args) {
    JFrame app = new MyApplication();
    app.setVisible(true);
  }
  ...
}

The problem here is that the app starts and interacts with the UI from an initial thread, not from the UI event dispatch thread. That setVisible call seems innocent enough, and I know this happens all the time, but really, the code should look more like this:

public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      JFrame app = new MyApplication();
      app.setVisible(true);
    }
   });
}

The problem with this example is that it's trivial. From the point of construction to the setVisible call, nothing significant is happening. In more complex construction sequences, there's potential for drawing errors, mismatches between model and views, etc. I suppose any number of things could go wrong. One problem is that most of the time, nothing goes wrong...not in something this simple. That means that people don't see a problem, don't suspect anything, and certainly don't feel the need to fix this.

However, the simple fact of the matter is that all Swing interactions should occur on the EDT...not doing so has been declared unsafe. That usually means that you must use the invokeLater method from non-EDT threads.

Using SwingUtilities, it's easy to create and start your app's UI on the event dispatch thread. Seems so simple, and it is. I might even be able to keep this resolution.


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Yes, I've experienced this. It's not a myth. The details are a bit fuzzy to recall, and it would be even harder to explain (and quite boring to read about, I bet). Have faith, and do the right thing.

    -Andy

    Posted by: detorres on January 12, 2007 at 11:40 AM

  • Dumb question: if Swing interactions must occur on the EDT, why doesn't swing detect whether or not you're *on* the EDT and raise an exception if you try to start an application on a thread that's not the EDT?

    Posted by: davidvc on January 12, 2007 at 02:18 PM

  • While in most simple cases you'd be able to walk away with a functional application, that is not true in general. For more complex applications you can end up in messed UI / deadlock (frozen UI).

    Posted by: kirillcool on January 12, 2007 at 05:16 PM

  • If not exceptions, assertions would definitely help debug EDT related bugs


    Dumb question: if Swing interactions must occur on the EDT, why doesn't swing detect whether or not you're *on* the EDT and raise an exception if you try to start an application on a thread that's not the EDT?

    Posted by: davidvc on January 12, 2007 at 02:18 PM

    Posted by: montechristos on January 13, 2007 at 02:50 AM

  • This aproach does not work with netbeans IDE and java web start module !!!
    If you launch your application from java web start it simply does not run !!
    There's no exception thrown, simply the frame does not show !!
    You can try it yourself .

    I was going crazy to debug this !!!

    (sorry for my poor english)

    thanks john for giving us such a great posts !

    Posted by: aleixmr on January 13, 2007 at 03:41 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds