9.2 Toolkit-agnostic Threading - Reference Documentation
Authors: Andres Almiray
Version: 1.2.0
9.2 Toolkit-agnostic Threading
Swing is not the only toolkit supported by Griffon. For those additional toolkits the three methods exposed in the previous sections (edt, doLater, doOutside) make no sense, however running code inside the UI thread in a synchronous/asynchronous way, as well as outside of it is something you must keep in mind.The following sections outline toolkit-agnostic threading options, which can also be used with Swing in case you're wondering. These methods are available to all classes that implement the griffon.core.ThreadingHandler, such as griffon.core.GriffonArtifact and griffon.core.GriffonApplication interfaces.9.2.1 Synchronous Calls
Synchronous calls inside the UIThread are made by invoking theexecInsideUISync{}
method. This method is equivalent to calling edt{}
in Swing.Example:class MyController { def model def action1 = { // will be invoked inside the UI thread by default (pre 0.9.2) def value = model.value Thread.start { // do some calculations execInsideUISync { // back inside the UI thread model.result = … } } } def action2 = { // will be invoked outside of the UI thread by default (post 0.9.2) def value = model.value // do some calculations execInsideUISync { // back inside the UI thread model.result = … } } }
9.2.2 Asynchronous Calls
Similarly to synchronous calls, asynchronous calls inside the UIThread are made by invoking theexecInsideUIAsync{}
method. This method is equivalent to calling doLater{}
in Swing.Example:class MyController { def model def action1 = { // will be invoked inside the UI Thread by default (pre 0.9.2) def value = model.value Thread.start { // do some calculations execInsideUIAsync { // back inside the UI Thread model.result = … } } } def action2 = { // will be invoked outside of the UI Thread by default (post 0.9.2) def value = model.value // do some calculations execInsideUIAsync { // back inside the UI Thread model.result = … } } }
9.2.3 Outside Calls
Making sure a block of code is executed outside the UIThread is made by invoking theexecOutsideUI{}
method. This method is equivalent to calling doOutside{}
in Swing.Example:class MyController { def model def action1 = { // will be invoked inside the UI Thread by default (pre 0.9.2) def value = model.value execOutsideUI { // do some calculations execInsideUIAsync { // back inside the UI Thread model.result = … } } } def action2 = { // will be invoked outside of the UI Thread by default (post 0.9.2) def value = model.value // do some calculations execInsideUIAsync { // back inside the UI Thread model.result = … execOutsideUI { // do more calculations } } } }
9.2.4 Additional Methods
There are two additional methods that complement the generic threading facilities that Griffon exposes to the application and its artifactsisUIThread()
- returns true if the current thread is the UI Thread, false otherwise. Functionally equivalent to callingSwingUtilities.isEventDispatchThread()
in Swing.execFuture(ExecutorService s, Closure c)
- schedules a closure on the target ExecutorService. The executor service can be left unspecified, if so a default Thread pool executor (with 2 threads) will be used.execFuture(ExecutorService s, Callable c)
- schedules a callable on the target ExecutorService. The executor service can be left unspecified, if so a default Thread pool executor (with 2 threads) will be used.