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 NetBeans.tv, 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 java.sun.com 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 Levy shares some lessons learned for working with Single Sign On.
"In the last few weeks I was asked to help to integrate a set of built-in-house web applications with a Single Sign On (SSO) solution. After working with people from different teams, I realized that it would be a good idea to write a brief description of how the SSO solutions work in general. Perhaps this might help you to get started if you have to do something like this at some point."

Fabrizio Giudici needs some media functionality and wonder
What about Movies?
In the latest days I've writing some code to investigate about how hard or easy would be to extend blueMarine capabilities to manage movies in addition to photos. While my personal interest and volunteering efforts about blueMarine are primarily photo-oriented [...] some modules are involved in my business and a customer asked me about movies for a digital asset management application. So I did some preliminary investigation work.

Tim Boudreau is taking NetBeans on the road. In
The NetBeansMobile - coming to a city near you?, he writes, "I'm going to doing a road trip - part me moving to the east coast of the U.S., part NetBeans World Tour. So I'm loading up this truck with NetBeans books and t-shirts (along with my earthly posessions). And it will have a web cam and gps tracking installed, and will regularly upload photos out the windshield and its location to a web site... Yes, this is truly geeky... I'd love to get together with folks interested in NetBeans or Java in places I stop...it will be the northern route, I80 from San Francisco to Chicago, I90 from there. I'll be starting off on the trip Friday or Saturday..."


In today's Forums,
mattnathan announces
JXClock + demo.
"Just committed the beginnings of a new component titled JXClock a relatively simple component that shows time: Demo here. The demo shows the current default theme (very basic) and two additional themes (Mac OS X and Microsoft Vista) and uses a model which shows the current system time down to an accuracy of one second. All demo themes use pure Java2D and can be scaled to suit your needs. The painting of the themes is in a rough-cut state and I expect can be re-jigged so that there is more code reuse and creating new themes becomes easier. I will also add configuration properties at some point so things like ticks, number labels and the second hand can be toggled. Feedback welcome."

resix has some
Questions on the phoneME VM for what sounds like an interesting project. "In the context of my PhD thesis I plan to extend a JVM by support for aspect-oriented execution mechanisms. Therefor, I am looking for a J(K)VM implementation which (at the best) can also be deployed to a 'real' limited device target. I consider to use the phoneME feature VM and have a few questions regarding this: 1. What are the main differences between the phoneME feature VM and the KVM reference implementation from Sun? Is there a documentation describing something about this? 2..."

ypomonh2
wonders
Can I access the WSDL from localhost..?
"At service creation can I access the WSDL from localhost or host, although the endpoints that it defines are located in another server eg. hostB? Are there any security restriction like the ones that apply on Applets or AJAX components, that would only allow me to communicate with the WSDLs' origin server? This way I could host the WSDL at localhost (or a server in my intranet) and avoid the expensive remote accesses of the WSDL which are required at runtime."


Current and upcoming Java
Events
:

Registered users can submit event listings for the href="http://www.java.net/events">java.net Events Page using our href="http://today.java.net/cs/user/create/e">events submission form.
All submissions go through an editorial review before being posted to the
site.


Archives and Subscriptions: This blog is delivered weekdays as
the Java
Today RSS feed
. Also, once this page is no longer featured as the
front page of java.net it will be
archived along with other past issues in the href="http://today.java.net/today/archive/">java.net Archive.

Unforseen hazards of the Swing event-dispatch thread