« Can We Get a Three State CheckBox in Java 7? | Main | Declarative GUIs in Java, Lysol for Code Smells »

Groovy vs. Cocoa: invoking a method on the main GUI thread

Todd Ditchendorf compares Cocoa and Java and C# when it comes to acessing the GUI thread in their respective GUI toolkit. He ranks the readability as Cocoa then Java then C#.

Here is the Cocoa version, is this readable?

//... inside class definition ...

- (void)doStuffOnWorkerThread {
    // ... do worker thread stuff, then...
    id arg = ...
    [self performSelectorOnMainThread:@selector(doStuffOnMainGUIThread:)
                           withObject:arg
                        waitUntilDone:NO];
}

- (void)doStuffOnMainGUIThread:(id)arg {
    // update UI, etc...
}

If you are into brackets, self-referential code, name:value, operators as scope modifiers, and methods that look like e-mail addresses then Cocoa is for you. I prefer something more Groovy...

arg = ...

doInThread = {
   // update UI, etc...
}

SwingUtilities.invokeAndWait doInThread

Even groovier...

arg = ...

SwingUtilities.invokeAndWait {
   // update UI, etc...
}

About

This page contains a single entry from the blog posted on February 7, 2007 5:02 PM.

The previous post in this blog was Can We Get a Three State CheckBox in Java 7?.

The next post in this blog is Declarative GUIs in Java, Lysol for Code Smells.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33