29 September 2005

Protect your GUI thread

He (Heinz) was right about it. We were in serious trouble not tohave restricted access to our gui components from other threads than the Swing thread.

Fortunately, our design is not so bad. Last year, I had the goodidea to use an EventBus to communicate between the business layer and the gui layer (or even between the gui components themselves). As a result the correction was quite easy and came in the form of a pattern:
  • the component does not register himself but registers a proxythat implements the same "EventReceiver" interface. When receiving abusiness event, the proxy forwards the call but by invoking aSwingUtilities.invokeAndWait().
This really solved the freezing bug that we had only observed once on one of our consultants computer.

However, the developper I was working with was dissatisfied with the solution. He would have preferred a "implement an interface" or"extend an abstract class" kind of pattern that would force thecomponent to be executed on the Swing Thread.

It made me think of 2 things:
  • why Swing does not throw an exception when a gui component is not executed on the Swing thread, since it is so dangerous?
  • could we use our components factory to insert another proxy that would check that any method call made on a GUI component is checked against this error?
I am going to investigate dynamic proxies for that matter (I hope not to have to resort to AOP,...) and will blog more about it (Santosh's blog seems to be an interesting start).

Just another thought came on top of my mind: using dynamic proxy involves the use of interfaces. Why don't we have our gui component simplement business interfaces exposing only the events they receive/send? Once a JTree is constructed, what other components need to see it as a JTree? Being a JTree after all is more a matter of implementation about how I want to see some kind of info.

That's all for today. The London exhibition was great and the testing market in UK seems interesting for us. I also felt very comfortable around English people, I don't really know why; maybe some kind of ever politeness floating around? I also met a fine magician, which is another subject I am also passionate about,...

No comments: