
[[_threading_outside_async]]
= Background Calls

Making sure a block of code is executed on a background thread is accomplished by invoking
the `runOutsideUIAsync` method. This method always runs the code on a background thread regardless
of the caller / invoking thread. This is usually performed by a helper `java.util.concurrent.ExecutorService`.

[source,groovy,linenums,options="nowrap"]
----
package sample

import java.awt.event.ActionEvent
import griffon.core.artifact.GriffonController
import griffon.core.controller.ControllerAction
import griffon.annotations.inject.MVCMember
import griffon.annotations.core.Nonnull

@org.kordamp.jipsy.ServiceProviderFor(GriffonController)
class SampleController {
    @MVCMember @Nonnull SampleModel model

    @ControllerAction
    void work(evt = null) {
        // will be invoked outside of the UI thread by default
        def value = model.value
        // do some calculations
        runInsideUIAsync {
            // back inside the UI thread
            model.result = ...
            runOutsideUIAsync {
                // do more calculations
            }
        }
    }
}
----

