Monday, March 10, 2008

On threading

I've been quiet for a while because I've been heads down working on some new features for NetBeans 6.1 (and now, debugging those features).

One class of bugs has been knocking me down a number of times: there is a chunk of code that is not thread-safe in a very "interesting" way. I am not historically a GUI programmer, and I have been exposed to the amazing intricacies of threading in a GUI application like NetBeans.

I won't go into details (AWT thread cough cough), but suffice it to say that I have had to put on my multi-threaded hat in deeper ways than I have had to for a while.

In the background I've also been reading about Scala, Erlang, threading and scalability. I will ask my readers to please help me, because I can't find the original references, but some comments I have been percolating in the back of my brain as I wade through my bugfixes.

First of all, someone posted that any language that required users to manage their own concurrency was a broken language. That got me thinking - hey, it is pretty crazy that programmers have to deal with this. With Java we got rid of most memory management issues (not all, think weak references), and we have a wonderful exception model, and very clear typing semantics (a reference can't suddenly become an int, for example, like it can in C). All in all it made a safer sandbox for programmers. But then here is this threading and locking complexity that has engendered books upon books, incredibly popular JavaOne talks and so on.

Then I keep hearing about Erlang and Scala, and in particular how they don't do threading, they do message passing, and there is absolutely no shared data between process units. Ah, a light came on in my head. If you follow this model, you avoid ending up in the ditch (or stuck in gridlock).

I remember working on an incredible product at Sybase called Open Server, ahead of its time in many ways, the invention of Tom Haggin, number three employee at Sybase, and someone I was honored to work with.

Open Server was a server that understood the Sybase client/server protocol called TDS, with an open API that let you plug in event handlers to process client requests and send results.

Since it was a server, it had to be multi-threaded. Its recommended mechanism for inter-thread communication was to avoid shared data and use in-memory message queues. You want to talk to another thread, you post a message on its queue. It was so simple and so effective, and so different from how things are done in most desktop applications like NetBeans. At the time I didn't think much about it, I just thought it was an interesting invention.

But my tribulations with Java and threading, my memories of the elegance of the message-passing model, and the ongoing challenge of scaling in multi-core systems, have reminded me of the power of this model, and increased my interest in languages like Scala and Erlang. Now if I can just pull myself out from under these bugs long enough to take a look at them...

No comments: