Skip to main content

Can't Trust It

Posted by editor on August 30, 2007 at 7:26 AM PDT

Unforseen hazards of the Swing event-dispatch thread

Care and feeding of the Swing event-dispatch thread is something that every new Swing developer hears about and reads about over and over again. And yet, mistakes with this are the cause of many subtle bugs and perceived slowdowns.

Why is it so hard to get this stuff right? Well, for one thing, it's easy to do. It's not always obvious what thread your code is running on -- an event handler sure, that's on EDT, but if you had a method that could be called via RMI, would you think of the threading implications of touching the GUI (or its underlying models) from there? And then there's the rule against not performing long-running actions on the event-dispatch thread. Something that runs nice and fast on your development machine or your internal network, fast enough that you don't realize you've done it on EDT, might behave very differently in the field, just because of different hardware, networking configuration, latency, etc.

Substance project owner Kirill Grouchnikov points out another reason this gets messed up so often: these rules of Swing threading, while so important, are not enforced in any way, neither by the compiler nor by the runtime. Given Java's usual philosophies of compile-time strictness and fail-early programming, letting this stuff just fall on its face at runtime could be seen as atypical of Java programming in general. And maybe that's why it's so easy to do: usually when we do stupid things, Java stops us, and does so early on. But a GUI-hosing race condition or a deadlock when two threads mess with a TableModel? What's going to stop you from doing that? Nothing, actually.

In our Feature Article, Kirill introduces some tools for Debugging Swing . Combining custom event-listeners with some JMX functionality to get thread information at runtime, Kirill shows how you can track down long-lasting actions on the EDT and figure out EDT deadlocks:

Swing is a very powerful UI toolkit. You can do pretty much anything you want with it, but it's very easy to abuse this power. Using the non-intrusive techniques described in this article, you can easily locate event listeners and other related code pieces that violate the EDT-related rules, making your application more responsive and robust.

In Java Today,
Issue 306 of the NetBeans Weekly Newsletter is out. Contents include: relaunch of, NetCAT 6.0, registration opens for NB Day Boston, Wade Chandler profile, extending the NetBeans Tutorial JSF-JPA-Hibernate Application, native Ruby development on NetBeans 6.0, how to write a Groovy editor, a creative use of the NetBeans visual library and much more.

Wondering what's up with the Java 7 Closures proposals? InfoQ has an update in Catching Up With Closures for Java: "Neal Gafter recently gave a presentation at JavaOne and Jazoon '07 entitled "Closures for Java". The presentation is an accessible but thorough introduction to closures, the goals, the problem with existing solutions, all presented in a conversational style."

The latest SDN Java Technology Vodcast is "Deep Dive: Project Tango, An Interview With Arun Gupta". Host Ed Ort, from the team, interviews Arun Gupta, Sun Technology Evangelist for Web Services and Next Gen Web Apps (a.k.a, Web 2.0): "Get insights into Project Tango and its importance. Learn about the specifications that comprise WSIT and that are implemented in Project Tango. Most important, find out how easy it is to create and use interoperable web services through Project Tango."

In today's Weblogs, Elie