Java Concurrency in Practice by Brian Goetz et al
I’d been hearing people mention this book for a long time, and took quite a while to actually get hold of a copy to read. It’s a very good book. Its strength is that is covers the Java concurrency related libraries in good detail, but more than this, there is a lot of supporting material on concurrency in general.
The second chapter of the book covers what it means for something to be thread safe. This term is thrown around a lot by developers, but the chapter makes it clear that there are lots of properties that you may require of a thread safe class, and it is surprisingly difficult to pin down exactly what thread safety is. The next two chapters of the book talk about the sharing and composing of objects, and how this affects their concurrent use. The last chapter of the introductory section covers the basic building blocks, including synchronised and concurrent collections, blocking queues, blocking and interruptible methods, and synchronisers. All of the topics are covered in depth, and I think most people would learn something from reading them.
The second section of the book is about structuring concurrent applications. It covers the notion of tasks, and the connected notions of cancellation and shutdown. The Java mechanism for running thread pools is covered, and this is followed by a discussion of the interplay between GUI applications and concurrency. This has a very well written section on why GUIs are single threaded, answering well the question of why do I always need to move onto the GUI thread before changing GUI elements.
Section three is on liveness, performance and testing. This covers livelocks, deadlocks, performance, scalability, Amdahl’s law and also has a good discussion of testing concurrent programs.
Section four, advanced topics, covers locks and moves on to a discussion of when you might want to use atomic variables and non-blocking synchronisation. There is also a section on the Java memory model and the subtle guarantees of the platform such as how it needs to ensure that publication happens correctly after a constructor runs. There is also a chapter on AbstractQueuedSynchronizer a class which acts a superclass for many of the Java library’s synchronization constructs.
This is a thoroughly interesting book, both explaining the Java library very well and providing a lot of general advice on the topic of concurrency, and is well worth a read.