Java Concurrency in Practice

Brian Goetz with Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, Doug Lea

There are few programming books that you could describe as virtually flawless, or whose every page is enlightening. Java Concurrency in Practice is one of the few Java programming books to have both of these qualities, and its availability on the Kindle is hugely welcome.

Before the Kindle version appeared, I would have said that if you're a serious Java programmer and don't have this book on your bookshelf, then you should do. Now you can choose between print and electronic versions, but either way, the content is both a must-read and a must-reference.

For those not familiar with the work, then, the underlying themes are the Java concurrency facilities introduced in Java 5 in the form of a new library of concurrency utilities and a tightening up of the Java Memory Model. Java 5 marked an important shift in direction for concurrent programming in Java. That shift was away from the relatively poor selection of lower-level constructs available— notably the synchronized keyword for data sharing and the wait/notify mechanism for inter-thread communication— towards giving Java genuine synchronization constructs within the standard library: queues, semaphores, atomic variables, barriers etc. Although previous releases of Java had included some important additions, notably NIO and regular expressions in Java 1.4, version 5 is the most significant release to date, and is testimony to the importance of concurrent programming with today's increasingly parallel hardware. It is surprising, then, that few if any works before JCIP had dealt with the new Java concurrency libraries in any depth.

If you do any kind of concurrent or multi-threaded programming, I pretty much guarantee that there will be various bugs sitting there right now in your code that this book will give you the knowledge to fix. After a brief introduction to the concept of concurrency, the first chapters, looking at thread-safety and sharing objects, hammer home various concepts that few programmers are probably aware of unless they're concurrency experts. For example, the chapters emphasise, with examples of both buggy and correct code:

Subsequent chapters then go on to look at how to build thread-safe objects and use thread-safe collections, including the new concurrent collections such as ConcurrentHashMap, how to implement a producer-consumer paradigm using the new queue classes, and how to correctly implement a concurrent result cache, showing some of the common errors that programmers tend to introduce. Pretty much three entire chapters are devoted to the executors framework, which to some extent replaces using "raw" threads as of Java 5. The chapters deal with various topics that will be important to many server applications, such as how to constrct and determine the appropriate size of a thread pool, and how to parallelise certain algorithms.

The section on Liveness, Performance and Testing deals with expected topics such as how to diagnose deadlocks and Amdahl's law, but includes various enlightening pointers on issues such as the measured cost of context switching and the scalability of various types of Java Map and blocking queue. It also touches on some rather more subtle points, such as how to avoid contention on random number generators that could otherwise skew the results of your test harness.

Finally, the chapter on Advanced Topics looks at explit locks (which I would actually not have classed as such an advanced technique, but I suspect that by demoting explicit locks to the "advanced" section, the authors wanted to deter programmers from overusing them) and how to build one's own synchronization constructs. An overview of the Java Memory Model, which in reality is a recapitulation of topics already covered earlier on, is also given in the final chapter.

All in all, there's very little to fault about this book, and either in electronic or print version, it's really a must-have for all serious Java programmers. Most readers will also appreciate its fairly practical approach to using the concurrency library.

For those who want to take their concurrency knowledge even a stage further, an excellent complement to Java Concurrency in Practice is Herlihy & Shavit, The Art of Multiprocessor Programming, which goes into more detail about the multiprocessor architecture, and includes more information on the theoretical background behind many of the various concurrency constructs that Java 5 offers you "off the shelf".