Which method uses a predefined sequence to acquire locks, preventing circular wait conditions?

Lock hierarchies are a technique used to avoid deadlocks by imposing a strict ordering on the acquisition of locks. Deadlocks occur when there is a cycle of resource dependencies, known as a circular wait condition. For example, if thread 1 holds lock A and waits for lock B, while thread 2 holds lock B and waits for lock A, a deadlock occurs.

By enforcing an order:

  • No Cycles: The predefined order ensures that threads cannot hold a lower-order lock while waiting for a higher-order lock, effectively breaking the circular wait condition. In our example, thread 1 could hold lock A but must acquire lock B (if needed) in the correct order.
  • Predictability: This approach makes the locking strategy predictable and easier to manage, as developers know the sequence in which locks must be acquired.