Java Concurrency in Practice

by Brian Goetz, Tim Peilers, Joshua Bloch, Joseph Bowbeer, David Holmes, Doug Lea

Writing scalable applications that use the capabilities of today's multi-core CPUs while running reliably and with high performance is a challenge. Race conditions, deadlocks, and other liveness errors quickly creep in.

Brian Goetz and a team of experts from Java Specification Request 166 "Concurrency Utilities" describe in "Java Concurrency in Practice" the fundamental concepts of thread safety and the correct implementation of thread-safe applications in Java.

The book is divided into four parts:

  1. The first part is about basics like atomicity and visibility, intrinsic locks ("synchronized"), safe publication of objects, thread-safe composition, and concurrency building blocks concurrent collections and synchronizers like semaphores, latches, and barriers.
  2. The second part is about parallelizing applications using tasks and the executor framework. It explains the configuration of thread pools and the safe termination of tasks, threads, thread pools, and applications.
  3. The third part is about liveness (deadlocks, livelocks, thread starvation), measuring performance and improving it by reducing lock contention, and writing tests for concurrent algorithms.
  4. The fourth part covers advanced topics such as explicit locks, condition queues (wait/notify), atomic variables, non-blocking algorithms, and the Java Memory Model. Building on this, the functionality of many of the topics presented in part one, such as object publishing, concurrent collections, and synchronizer classes, is explained.

All explanations are accompanied by excellent real-world code examples. Most show frequently made but incorrect implementations first, followed by the correct ones.

The authors successfully explain an extremely complex topic in a relatively simple way. Of particular note is the excellent explanation of the Java Memory Model and the "happens-before" relationship.

Nevertheless, the book is not an easy read, and it takes time to understand all the details. I recommend reading the book at least twice.

Even though the book is on the level of Java 6, it is still up-to-date. The basics of thread safety have not changed since, and all features added in later Java versions (like the fork/join framework, CompletableFuture, or parallel streams) are based on them.

🎧 Suitable as an audiobook? No, due to numerous extensive code examples.

P.S.: If you want to go deeper into the advanced topics and read about specific types of memory fences and advanced memory access methods like release/acquire and opaque, I recommend Doug Lea's "Concurrent Programming in Java" as a follow-up reading.

Other Recommendations From This Books’s Genres

* Disclosure: We love sharing our favorite books with you! As an Amazon Associate, we earn a small commission from purchases you make through our links, which helps us continue creating content you enjoy.