42 CONSISTENT LOCK ORDER

DESCRIPTION

During the discussion of the Monitor and the Critical Section patterns earlier, we have seen that when the synchronized keyword is used to ensure single-threaded execution of a code block, a thread needs to wait while trying to acquire the lock associated with the specified object. Consider a scenario where two threads hold locks on two different objects and each one is waiting for a lock on the object that is locked by the other thread. Both threads will be waiting forever and are said to be in a state of deadlock. In terms of implementation, this type of situation most often occurs due to an inconsistent order of locking objects. Let us consider the code segment in Listing 42.1 to illustrate how inconsistent ...

Get Software Architecture Design Patterns in Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.