Programming Concurrency on the JVM

15 Feb

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

Are you an experienced Java programmer? Do you want to explore your options of managing and making use of concurrency on the JVM using either Java or other JVM languages such as  Scala, Clojure, Groovy and JRuby? Or maybe you’ve heard about the Software Transactional Memory (STM) or actor-based model and are wondering if there is a way you could make use of them while still writing for the JVM? If so, this book is for you.

The book is the fourth one by the same author featured by the Pragmatic Bookshelf. As it was the case for all books under this label that I’ve read, this book is pleasing to read. 280 pages make it not very long and a good part of it is code. The book is available both as a paper book and as an eBook, in three formats: epub, PDF and mobi/Kindle.

The author uses simple but nicely chosen problems as a basis for the discussion of different approaches to concurrent programming. The source code for examples used in the book is available from the publisher’s site. Unfortunately, these are not runnable programs. For instance, in java files, there are packages declared, but there is no corresponding folder structure. So, in order to run the code, you will need to move some bits around. This is an inconvenience that could have been easily avoided.

The book is well structured and besides the usual introduction and summary parts, consists of four parts: “Strategies for Concurrency”, “Modern Java/JDK Concurrency”, “Software Transactional Memory” and “Actor-Based Concurrency”. I found the “Recap” paragraph at the end of every chapter very useful. It provides nice summary and will be a useful reference when reaching for the book next time.

The book starts with highlighting the problems that concurrency is trying to solve and points that all the benefits are not for free – “The Power and Perils of Concurrency”. What follows is a discussion of possible ways of turning your sequential program into concurrent one with consideration of whether the application is IO or computation intensive and how it affects the number of threads needed. One of the first questions you will need to answer when writing a concurrent application is how to deal with state. The author quickly explores three available options: shared mutability, isolated mutability and pure immutability. These first chapters nicely prepare the ground for further parts.

In the “Modern Java/JDK Concurrency” part, the author looks at the new threading APIs introduced in Java 5 and later. A message for a reader is to forget about old threading APIs and benefit from:

  • new and better ways of managing a pool of threads and scheduling tasks for concurrent execution,
  • fine-grained synchronization using locks,
  • new concurrent data structures

The section on Java 7 Fork-Join API is a definite sign that the book is up to date.

As a nice bonus, at the end of this part, the author presents a small program with a few flaws and walks through the process of eliminating them.

As the author suggests himself, the book just gives an overview of solutions available in Java and the JDK. If you need one that goes more into details you should grab “Java Concurrency in Practice” by Brian Goetz.

Almost half of the book is taken by parts on Software Transactional Memory and Actors. These parts have similar structure. There is one chapter devoted for introduction and discussion of the concept, with examples in one or two of the languages on the JVM. The chapter following shows how to implement a solution in different JVM languages, often by using a library discussed previously. The author diligently highlights potential problems when using libraries across JVM languages and shows how to work around them. I personally liked the few pages of theory before digging in to the code examples.

STM is explained in the context of Clojure which has support for it built into the core language. We are shown how STM enables lock-free programming and eliminates the need for synchronization. The author discusses suitability of STM for different problems and shows with an example that it is not meant to be used in applications that have a lot of write collisions. The book also covers an alternative implementation of STM in Java – Multiverse and support for it in Akka.

In the part on actor-based concurrency, the author uses the Java and Scala APIs of Akka to walk us through different types of actors, ways of exchanging messages between them and coordinating them. Since the examples in Java and Scala are not that different, I think it would be better if only one language was used in the introduction chapter and the examples in the other language were in the chapter that follows. Having examples in both slightly distracts. As it was the case for STM, the author concludes the chapter with a discussion of the limitations of the actor model.

The second chapter in this part contains examples in JRuby, as well as in Groovy using GPars.


Although not my daily bread, I write some concurrent code every now and then. I reached for this book mainly to learn about STM and actors and see when and how I could benefit from these approaches to concurrency. And I was not disappointed. The author did very good job in explaining them and provided a comprehensive overview of available implementations.

Regarding the title, I would say that the word “mastering” may be a little overstatement. I think the book is an excellent starting point and once you choose an approach for you problem there is further reading to be done.

The book provides nice coverage of options available when programming concurrent applications on the JVM, and although the implementation might not be written in the same language you are working with, they are still very viable options.

I really enjoyed reading the book.

About the Reviewer

Tomasz Wróbel is a developer who spends most of his work day building web applications in Java.  In his free time he explores functional programming with Scala and plays a little bit with Android.


One Response to “Programming Concurrency on the JVM”

  1. Chris Kelly February 16, 2012 at 5:52 am #

    Good review. I totally agree that this book summarises and provides avenues for further investigation rather than being an in depth treatise of concurrency. Brian Goetz’s book is the current flag bearer on traditional Java concurrency although as it been around for a few years it does not cover the new Java 7 fork/join functionality.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: