The Griffon Trove: checking EDT violations - No Fluff Just Stuff

The Griffon Trove: checking EDT violations

Posted by: Andres Almiray on June 18, 2012

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!

Andres Almiray

About Andres Almiray

Andres is a Java/Groovy developer and a Java Champion with more than 20 years of experience in software design and development. He has been involved in web and desktop application development since the early days of Java. Andres is a true believer in open source and has participated on popular projects like Groovy, Griffon, and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member of the Griffon framework and Hackergarten community event. https://ch.linkedin.com/in/aalmiray

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »