Thursday, August 25, 2011

Review of Venkat Subramaniam's new book on Concurrency

Programming Concurrency on the JVM: Mastering Synchronization, STM and Actors
by Venkat Subramaniam

This book should be considered part of a two book series along with Venkat’s 2009 book “Programming Scala: Tackle Multi-core Complexity on the JVM”. The earlier book was a relatively short (250 pages versus 852 pages for Odersky’s Scala book), perhaps too short, overview of Scala. It’s 12 chapters skip over lots of the details in Odersky’s 18 chapter book, and contains only a single chapter on Concurrent Programming.

By comparison the new book is all about Concurrent Programming models and attempts to be agnostic about the JVM language to be used. Most examples are first given in Java, then a simpler way is shown to do it in Scala, and then often a short section saying how the example could be done in Ruby.

The book describes the difficulties of concurrent programming in JVM languages especially given the rise of multi-core processors. It then describes three “solutions”: the na├»ve Java approach, Software Transactional Memory and Actors. It should be noted that the Actors chapter is based on Akka, so if you don’t care for Akka you may not like this book.

The Power and Perils of Concurrency – This should be an unsurprising recap of well known problems; if you’re reading this book you will almost certainly have run into the problems listed here already.

Strategies for Concurrency – Talks about ways to divide problems into parts that can be computed independently

Modern Java/JDK Concurrency – This talks in detail about the changes made to the Java libraries to better support concurrency, specifically things like using ExecutorService rather than “new Thread()” and using Lock vs. synchronized. Interestingly though the chapter fails to discuss the little known but serious differences between synchronized blocks and methods involving fairness and barging.
Taming Shared Mutability – This chapter begins to offer the Scala kool-aid of mutability is bad. Reading this chapter is made easier if you’ve already read Goetz’s “Programming Concurrency”.

Software Transactional Memory – This chapter talks about Clojure’s STM model and how to use it in Java via Akka. Its an interesting chapter but one that will feel odd if you’re a programmer also coming to terms with non-relational databases. As databases move away from the transitional transactional model here comes STM bringing transactions to mutable variables.

Actor-based Concurrency – Actors are basically a message passing schema where immutable messages are passed between lightweight actors (thread-like). Just as you can use thread pools to reduce the cost of threads there are what amount to actor pools which lighten their cost. It does require some rethinking to embrace the actor approach.

Overall this is a fine book that brings quite a lot to the table. My only reservation would be that if you are a seasoned Java programmer you might take the first four sections as a given and jump right to the Actors approach. And if you’re not a seasoned Java programmer you probably don’t realize how badly you need this book.

No comments:

Post a Comment