Chapter 16. Synchronization
Kevlin and Frank at JAOO 2006, synchronizing their watches © Mai Skou Nielsen
Concurrency mechanisms for dividing up and executing tasks across different threads present only one part of the concurrency story. Where objects are shared between threads, there is the question of thread-safe use of their methods. This chapter presents nine patterns that address synchronization or reduction of state change to minimize the need for synchronization.
In Chapter 15, Concurrency we discussed how the structure, efficiency and responsiveness of software for distributed systems can benefit from concurrency. We also showed that well-structured, efficient, and responsive concurrent software is surprisingly hard to design: 'Multithreading is one thing after, before, or simultaneously with another' [MeAl04a]. To address these challenges, we therefore included patterns for effective concurrent design in our language. But not only is designing concurrent systems challenging, programming them is too—when present, concurrency is a concern that runs through all levels and across all aspects of an architecture.
One reason why concurrent programming is harder than sequential programming is the need to synchronize access to shared resources. Threads that run concurrently may share access to the same objects. Without appropriate safeguards, therefore, methods that change the internal ...