Thinking Parallel

A Blog on Parallel Programming and Concurrency by Michael Suess

A Warm Recommendation for “Java Concurrency in Practice”

Today I would like to talk about a really good book. I have had it laying on my desk for some time and have not gotten around to reading it fully (only skimmed through it), mostly because Java was not at the very top of my priorities. But now that I have read it, I must say it is a really enlightening book not only for people wanting to educate themselves about Java threads, but for everyone who wants to mix object orientation and threads – which can be quite a challenge. I am talking about the book JAVA Concurrency in Practice by Brian Goetz and others.

Let’s start with what probably interests you the most – its content:

  • Ch. 1 – Introduction: this serves as a basic overview over the topic of threads, briefly motivates what is to gain and what is to loose by using them and goes on to show that threads are an integral ingredient of most Java programs already – whether you know it or not.
  • Part 1: Fundamentals
  • Ch. 2 – Thread Safety: Isn’t it nice to see an important topic like this covered at the very beginning of a book? And even with a whole chapter! It goes beyond simply defining what thread-safety is, but also adds practical guidelines how to achieve it, especially in an object oriented context.
  • Ch. 3 – Sharing Objects: Another important topic discussed right where it belongs. When is an object visible to others and guaranteed to be in a defined state? This chapter introduces the memory model without going into the gory details. Since this is an area where I know my students make many mistakes, I appreciate it being discussed so prominently.
  • Ch. 4 – Composing Objects: this shows, how to build classes from thread-safe building blocks without compromising thread-safety. Sounds trivial when you read it here, but (as always) the devil lurks in the details.
  • Ch. 5 – Building Blocks: what is provided by the Java runtime library with regards to concurrency? This includes everything from the java.util.concurrent package introduced in Java 1.5.
  • Part 2: Structuring Concurrent Applications
  • Ch. 6 – Task Execution: this shows how to exploit task-based parallelism using the Executor framework
  • Ch. 7 – Cancellation and Shutdown: how to make threads stop what they are doing gracefully
  • Ch. 8 – Applying Thread Pools: covers more advanced techniques for task-based parallelism, including how to fine-tune the Executor framework even further
  • Ch. 9 – GUI Applications: shows why most most GUIs are single-threaded and how to deal with that in a multi-threaded world
  • Part 3: Liveness, Performance and Testing
  • Ch. 10 – Avoiding Liveness Hazards: Deadlocks, starvation and livelocks are explained in this chapter, along with ways to avoid them
  • Ch. 11 – Performance and Scalability: can’t live without them :razz:. Premature Optimization, Amdahls Law and of course the difference between performance and scalability are covered here (among other topics), along with ways to increase both, e.g. by decreasing lock contention. Some of it reminded me very much of one of my first articles about high-level optimization.
  • Ch. 12 – Testing Concurrent Programs: Things just keep getting better and better. This is actually the first book I know that has a focus on testing in a concurrent environment. And while the tips and tricks shown here are not exactly rocket-science, I wish I could have read about them sooner in my career instead of discovering them from scratch.
  • Part 4: Advanced Topics
  • Ch. 13 – Explicit Locks: Introduces explicit locks and Read-write locks and shows when they may be needed and how they compare to the implicit locks in synchronized.
  • Ch. 14 – Building Custom Synchronizers: We are entering the basement in this chapter and are exploring the very basic synchronization constructs that can be used to create custom synchronizers: condition queues, objects and so called AbstractQueueSynchronizers. My personal guess is, most people will never need them, as Java provides a rich API on top of them. But it is still nice to know they are there and accessible when needed.
  • Ch. 15 – Atomic Variables and Nonblocking Synchronization: The fact that Nonblocking Algorithms are possible to write in a portable manner in Java is in itself very good news (well, in fact, this news item is not really new anymore, as the functionality has been there since Java 1.5). This chapter shows some examples of such algorithms and implements some simple, non-blocking data structures. It also warns that developing correct algorithms this way is difficult – and I very much agree.
  • Ch. 16 – The Java Memory Model: I have not even read this chapter yet. Memory Models are hard to understand and are even harder to understand correctly and fully, as I have learned the hard way from OpenMP. The basics of the Java Memory Model are explained in an earlier chapter of the book, therefore I will safe this part until I either feel really curious and have some time to spare or until I am actually implementing performance-critical, concurrent Java applications and am in dire need for a performance boost.

Pretty comprehensive, don’t you think? All of the content is well-presented, a joy to read, includes helpful examples and is up-to-date (Java 1.6 is covered). The authors were heavily involved in the creation of the java.util.concurrent package introduced in Java 1.5., which is why they obviously know what they are writing about :lol:.

What this book is not is an introduction to Java. More than once during my time reading the book I noticed that my Java was quite rusty and I had to look something up in the Java Documentation to make sense of it. I expect you to do better in this regard. 😀

Nevertheless, for people interested in parallel programming with Java this book definitely has the potential to become the reference on the topic. Joe Duffy recently said about it in his blog:

It’s not as good as my upcoming book, but given that you can’t buy mine yet, it will do for now ;).

Joe has set himself a very high standard to live up to, and I am certainly looking forward to seeing how his book compares, especially since .NET could use a good book on threading…

4 Responses to A Warm Recommendation for “Java Concurrency in Practice”


Comments