| «« Java Microbenchmarks are Evil | Black Box vs. Crystal Box Development and Security »» |
|
About
I'm Ryan Lowe, a Software Engineering graduate living in Ottawa, Canada. I like agile software development and Ruby on Rails.
I write this blog in Canadian English and don't use a spell checker. Typos happen.
Projects
» Full-time Ruby on Rails freelancer
» Full-time with Rails since May 2005 » Former committer for RadRails (now Aptana) » I also have a few Rails side-projects in development: 1. wheretogoinTO.com Toronto nightlife 2. Hey Heads Up! TODO list and sharing 3. Layered Genealogy family history research 4. foos for foosball scoring 5. fanconcert for music fans (on hold) Hiring Rails developers? I can telecommute by the hour from Ottawa, Canada »» Email: rails AT ryanlowe DOT ca
BulletBlog
Now hosted on Hey! Heads Up -- check it out!
Syndication
Pings
Recent
Derek Lowe's (Ryan's older brother) words at Ryan's funeral
blog@ryanlowe.ca no more Forging Email Headers: Good, Bad or Ugly? Sarcastic Dictionary (Part 1 of Many) Tags Hierarchies Twisting Rails is Risky Business Risky Business? My Take on Early Alphas Whoa, it's August 2007 Closing Comments A Postscript to "Growth at the grassroots" »» All Blog Posts
Linkage
del.icio.us/ryanlowe
technorati/ryanlowe.ca/blog Aurora Roy Jim Andrew Trasker Travis Kibbee Karen Dr. Unk Ayana Van Bloggers Joel Spolsky Robert Scoble Tim Bray Dave Winer Raymond Chen James Robertson Ruby/Rails Bloggers rubyonrails.org weblog David Heinemeier Hansson Dave Thomas James Duncan Davidson Mike Clark Jamis Buck Signal vs. Noise Tobias Luetke Amy Hoy: (24)slash7 Jeremy Voorhis Eclipse Bloggers Planet Eclipse EclipseZone Luis de la Rosa Eclipse Foundation Kim Horne Billy Biggs Ian Skerrett Mike Milinkovich Bjorn Freeman-Benson Denis Roy
Archives
|
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:
if (tableViewer != null) 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 ESTLast 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 PMActually 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 PMSorry 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 |