Griffon: threading management - No Fluff Just Stuff

Griffon: threading management

Posted by: Andres Almiray on May 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!
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 »