chapter eight

8 Many cooks in the kitchen: Thread safety

 

This chapter covers:

  • Recognizing and avoiding deadlocks and race conditions
  • Using explicit locks
  • Using lock-free synchronization
  • Designing immutable classes

The plan for this chapter is to make your implementation thread-safe. For a class to be thread-safe, multiple threads should be able to interact with the objects of that class with no explicit synchronization.

In other words, a thread-safe class takes care of the synchronization issues. The clients can just freely invoke any class method, even simultaneously on the same object, with no adverse effects. The design-by-contract methodology presented in chapter 5 allows you to precisely characterize what an adverse effect would be: the violation of a post-condition or an invariant.

Compared with other functional defects, lack of thread safety can go unnoticed for much longer. Some synchronization defects become apparent only in special circumstances, when the timing and the scheduling are just right (or wrong) for a race condition to mess up the state of an object or for a deadlock to freeze your program. That’s one more reason to read this chapter carefully!

This chapter assumes you have familiarity with basic multi-threading in Java, such as creating threads and using synchronized blocks to achieve mutual exclusion. As a self-test, consider taking exercise 1 at the end of this chapter. It’ll remind you the main properties of the synchronized keyword.

8.1  Challenges to thread safety

8.1.1  Levels of concurrency

8.1.2  A concurrency policy for water containers

8.2  Dealing with deadlocks

8.2.1  Atomic lock sequences

8.2.2  Ordered lock sequences

8.2.3  A hidden race condition

8.3  Thread-safe containers ThreadSafe

8.3.1  Synchronizing connectTo

8.3.2  Synchronizing addWater and getAmount

8.4  Immutability Immutable

8.4.1  The API

8.4.2  The implementation

8.5  And now for something completely different

8.6  Real-world use cases

8.7  Summary

8.8  Applying what you learned