«« Java Microbenchmarks are Evil Black Box vs. Crystal Box Development and Security »»
blog header image
The SWT Thread

One of the key things no one seemed to tell us about SWT was that it ran in one thread by default. This has the unfortunate side-effect of freezing the GUI completely when you call a lengthy method from an event handler. This may seem like a big duh! to some people but I don't recall Microsoft Visual Studio GUI event handlers requiring explicit threading. It seems like a definite control versus ease-of-use tradeoff.

This is exactly where a university education falls short: practical information. We covered the basics of threads in second year but even then we only brushed on it briefly, and never with a GUI. When you step in and try to use SWT in a production environment you're completely clueless. No wonder the application was thrashing so badly, geez. Now because I'm using another thread the GUI is as smooth as silk.

The mutator event listeners that I implemented all cascade on the same thread: from the mutator to the controller to the model to the view when a change is made by a mutator. You want to call the mutator methods in a new thread from a GUI event handler so that the GUI won't freeze. When the listeners are notified that the mutator made a change when it gets back to the view it throws SWTException because you tried to access/modify the GUI objects from a thread that is not the SWT thread -- the same one doing the mutating also notifies and handles the listening.

So the last listener method (which still runs on the mutator thread) in the view has to notify the SWT thread that it wants to make a change. It does this by passing a Runnable object to SWT's display object:


public void itemRemoved(final ModelEvent me)
{
   final TableViewer tableViewer = this.viewer;

   if (tableViewer != null)
   {
      display.asyncExec(new Runnable()
      {
         public void run()
         {
            tableViewer.remove(me.getItem());
         }
      }
   }
}

Notice the way that Runnable interface's run() method is implemented inline. The objects that I used in the inline (tableViewer and me) must be final (constants) or the compiler will complain.

There were two separate ways I got around that. First, the me object was passed as a parameter to the listener method that contains this inline object. So I just made the parameter final in the method header. Second, the viewer changes in the object that contains this inline object, so before I make the inline object I just make a new final copy of it to tableViewer and use it instead of the non-final version.

Note that having the asyncExec() call in the view (actually it's the TableViewer's content provider) puts all of the SWT code togther in the user interface, rather than having some below it the GUI talking to the SWT thread which would increase coupling by putting SWT objects in the application code.

If you want to see how I implemented threading in AudioMan, it's in the CVS repository under the project AudioManPoint2. There are all kinds of race conditions now because methods that should be synchronized, especially in the repository, aren't yet. I'll be working on that this week -- I just wanted to see if the asyncExec() call worked, and it does.

Posted at February 15, 2004 at 04:17 AM EST
Last updated February 15, 2004 at 04:17 AM EST
Comments

I seem to recall Alex and Ron having an issue with only one thread for the GUI. They came across it doing one of the security assignments I believe. Basically I am just commenting on the fact that in University we weren't told about threads and race conditions in depth but more than likely we came across it at some point. Parallel helped with understanding of parallel execution (and if it didn't...are you sure that you took parallel?)

» Posted by: James at February 15, 2004 03:42 PM

Actually I did not take the parallel class. All I was saying there is that it would nice if practical stuff was actually in the curriculum, rather than leave it to chance that you *might* learn it at some point.

But that seems to be the difference between university and college in Canada: University teaches you how to learn and college teaches to how to do things. University students just have to learn a lot on their own.

» Posted by: Ryan at February 15, 2004 05:28 PM

Sorry about that, I should have mentioned that the comments in brackets were for those who took parallel. (Jim and Peter ;) )

» Posted by: James at February 16, 2004 04:46 PM
Google
 
Search scope: Web ryanlowe.ca