Tuesday, September 18, 2007

Java 5 Concurrency

Now that I have a little extra time, graciously granted to me because of my completion of two paper submissions (ICSE and WICSA), I thought I'd point out some cool things.

The Java standard library has at least two concurrency features that I was unaware of, and am now intrigued by.The first feature (new in Java5) is atomic objects. Java's atomic objects make the development of non-blocking algorithms and data structures actually possible, by providing atomic compare and set operations for primitive and object types. The neat thing about their implementation is that they automagically use the fastest underlying implementation available on your hardware, which is neat, since sometimes really fast compare and swap primitives actually exist.

The other feature is Futures, which allow delayed, concurrent computations. Futures have been a feature of functional programming languages for a while (and honestly, are probably a more appropriate abstraction in their world), but are still neat nonetheless. They allow you to compute some value off in the background, in a parallel thread. When you need the result of that computation, the library blocks until it has finished. So in practice, you get a little bit of extra concurrency without much intellectual overhead.

The point is, if you're programming concurrent code in Java, there's a lot of stuff already available, and it's worth your time getting familiar with the library in order to avoid reimplementing anything.