SpringOne 2GX 2011

Chicago, October 25-28, 2011

Griffon: threading management

Posted by: Andres Almiray on 05/25/2010
Right after Griffon 0.0 was released I made a post on the threading options it exposed. To recap, you can use the following methods on any View or Controller
  • edt {} - makes a synchronous call inside the EDT.
  • doLater {} - makes an asynchronous call inside the EDT.
  • doOutside {} - executes code outside the EDT.
However since Griffon is also able to run other toolkits besides Swing it became apparent that these threading constructs were not adequate, at least in their name; that's why a new threading abstraction was added in Griffon 0.3, the UIThreadHelper.

This class provides the same behavior as the previous threading facilities but with the added advantage that it is toolkit aware, meaning it will use the proper threading facilities as required by the running toolkit. These are the main methods you will find on this class
  • execSync {} - makes a synchronous call inside the UI thread.
  • execAsync {} - makes an asynchronous call inside the UI thread.
  • execOutside {} - executes code outside the UI thread.
  • isUIThread() - queries if the current thread is the UI thread.
  • execFuture {} - schedules the code on an ExecutorService and returns a Future.
As you can infer from the descriptions of the first three methods, they are functionally equivalent to the Swing methods. The remaining methods complement what you can do. Starting with Griffon 0.3.1 all of these methods can be called by Views and Controllers (actually any artifact that belongs to an MVC group to be more precise).

Calling these methods from another class requires you to access the singleton instance of the UIThreadhelper, in other words you need to write something like

UIThreadHelper.instance.execSync { /* your code */ }

In the upcoming 0.3.2 release you'll be able to call these methods inside the life cycle scripts using the shorthand notation, no need to use UIThreadHelper.instance as a prefix. It is still undecided if a functional equivalent to SwingWorker will be added to the threading options or not, at the moment the only abstraction around that class is SwingXBuilder's withWorker() node.

You will find more information about threading in Griffon by browsing the Griffon Guide.

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 »

NFJS, the Magazine

December Issue Now Available
  • BDD and REST

    by Brian Sletten
  • Mocks and Stubs in Groovy Tests

    by Kenneth Kousen
  • Algorithms for Better Text Search Results

    by John Griffin
  • Knowns and Unknowns of Scrum and Agile

    by Brian Tarbox
Learn More »