Monday, March 31, 2008

Immutable locks

Immutability is something that's mentioned over and over when it comes to parallel programming. Part I of Java Concurrency In Practice is all about composing objects that "play well" in the concurrent world and those that are immutable are the "best" citizens. No wonder functional languages are coming up in a big way. Anyone want to bet on them taking over the world in the next decade? Or if not taking over outright, at least succeeding in mutating our beloved imperative ones into unrecognisable functional beasts.

Anyway, check out the method below:

public class Foo {
    private Listener[] listeners;

    public Foo() {
        listeners = new Listener[0];
    }

    public void addListener(Listener listener) {
        synchronized (listeners) {
            Listener[] newListeners = new Listener[listeners.length + 1];

            for (int i = 0; i < listeners.length; ++i) {
                newListeners[i] = listeners[i];
            }

            newListeners[listeners.length] = listener;

            listeners = newListeners;
        }
    }
}


So here, the first thread to obtain the lock on listeners reassigns it to a new Array object... newListeners. Subsequent threads would continue to lock on the old listeners array, while new threads would lock on the "new" listeners array, and potentially corrupt the data. So I guess there's an unwritten property about locks... they need to be immutable to avoid situations like the above.

So given that locks need to be constant, there's no way to have a workable solution in the above code without using an additional object as the lock. The easy "lazy" thing to do is to just synchronize the method itself. The Foo instance (this) would then be that "additional object". But that would be wasteful since it would prevent all other synchronized method calls, even ones that have nothing to do with listeners.

(If instead of an Array, a List was used, the problem wouldn't have occurred given that the List itself wouldn't change. And I think, nowadays mostly the thinking when programming in managed languages is to go for Collections, rather than mess with manually managing Arrays. But I have still seen code like the above, so it's not totally contrived.)

Sunday, March 30, 2008

Constant Learning with Miro

In every field, it's important to continue to learn about one's discipline even after graduating from formal learning institutions. No where is this more pertinent than in the software industry where the only constant is (fast and furious) change. There's always books and articles to keep up with what's new, but videos have emerged in a big way as another avenue.

And it seems like the developer communities at all the big guns... Microsoft, Sun, Google and Yahoo... have jumped on the bandwagon and are producing great content. Microsoft had started channel 9 a few years back where they'd go around interviewing key engineers. Sun has videos through the Sun Developer Network Channel. Google has their Tech Talks series. Rahul introduced me to Yahoo's YUI Theater few months back.

The great thing about a lot of the talks is that although they are presented in the context of the company's platform/language/technology, they tend to transcend them and are concepts and trends that apply to general software engineering. Microsoft's channel9 for instance, has had wonderful discussions with architects and designers on everything from functional programming to garbage collection to concurrency.

In spite of all the great videos out there, it's a hassle to have to go to each of these different sites, see what's new and watch it. Enter Miro. Miro is an amazing open source application designed specifically to consume vidcasts (it works with podcasts too). Here's a screenshot where I've subscribed to some of these "channels" (yes, I have a lot of catching up to do!).



You may be wondering if this really is a huge deal. I think it is. Miro simplifies and automates the process and makes it so simple and easy. Every time you start miro, it'll tell you of the latest content available and ask if you want to download it. And the quality is generally much better what with being able to view it full screen and it also has nifty features like remembering where you left of in case you have to pause midway through etc... The one downside is that it has to download the video bits as opposed to streaming it through flash (as is common on many sites since the emergence of youtube). So for the bandwidth constrained it can be a bit of an issue. But then again, flash still has some issues on Linux and some of the videos don't play. Plus, looks like Microsoft recently converted to Silverlight which doesn't work on Linux (haven't looked into Moonlight). So most likely you'd end up having to download it anyway.

I've only mentioned few channels, but as you can imagine, there's tons of them around the web. Miro has a guide built-in which showcases some of them. Apart from tech content, many universities like Berkeley and Princeton are broadcasting some of their lectures and events. So there's definitely no lack of content. Just need to make time!

Overall, I love Miro and can't say enough good things about it. Kudos to the guys who've developed it.