The Griffon Trove: checking EDT violations

Posted by: Andres Almiray on 2012-06-18 05:05:00.0

Executing code in the proper thread is a must for every Swing application, problem is that it's so easy to break the rules. Griffon is aware of this and tries its best to help you deal with the complexity of running code in and out of the EDT.

Since version 1.0.0 of the Swing plugin you can instruct the running application to warn you when an EDT violation occurs. Take the following code for example

griffon-app/models/sample/SampleModel.groovy
griffon-app/views/sample/SampleView.groovy
griffon-app/controllers/sample/SampleController.groovy
Running this application with the EDT violation checker enabled throws some interesting results when the button is clicked.The controller performs 3 tasks

  1. Check if the current thread is the EDT or not. By default all controller actions are executed outside the EDT so this check should succeed.
  2. Change the value model.text which should trigger a change in the UI. Groovy bindings are aware that the change will be propagated to an UI component, sending the value in the correct thread.
  3. Change the button's text directly. This change breaks the Swing rule which is why we get a logged exception.
The lesson learned here is that Groovy bindings do the right thing for you. In the event that you need to update UI components directly then make sure you use either execInsideUISync or execInsideUIAsync, like thisMuch better. The Swing plugin has another ace up its sleeve: the ability to detect when an operation has run for too long inside the EDT. When this happens the UI becomes unresponsive and that's a big no-no. Updating the controller with a pause inside the EDT does the trick, say, let the UI freeze for 2 secondsRunning the application with the EDT hang monitor enabled and clicking the button yields the following resultPerfect, the long running operation was detected by the hang monitor. The default timeout is set to 1s however you can change this value if needed. All of these properties must be enabled when booting the application, changing their values while running will have no effect at all, that is, you can't deactivate/activate neither the EDT violations checker nor the EDT hang monitor at will once the application is running.

Keep on Groovying!


About Andres Almiray

Andres Almiray

Andres is a Java/Groovy developer and Java Champion, with more than 11 years of experience in software design and development. He has been involved in web and desktop application developments since the early days of Java. He has also been teacher of computer science courses in the most prestigious education institute in Mexico. His current interests include Groovy and Swing. He is a true believer of open source and has participated in popular projects like Groovy, Griffon, JMatter and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member and current project lead of the Griffon framework. He blogs periodically at http://jroller.com/aalmiray. You can find him on twitter too as @aalmiray. He likes to spend time with his beloved wife, Ixchel, when not hacking around.

More About Andres »

SpringOne 2GX

October 15 - 18, 2012

Washington DC

Current Topics on the NFJS Tour

  • Core Java, JEE
  • Dynamic Languages: Groovy, JRuby, Scala, Clojure
  • RESTful Web Apps
  • Frameworks: Hibernate, Grails, Spring, JSF, GWT, more
  • Agility
  • Test Driven Design
  • Security
  • Ajax, Flex, RIA
View Event Details »

NFJS, the Magazine

2012-12-01 00:00:00.0 Issue Now Available
  • Functional Thinking Part 1

    by Neal Ford
  • Better Together Than Alone: Pull Requests

    by Tim Berglund
  • XSLT in Groovy: Quick and Dirty

    by Nick Watts
Learn More »