Type a comment illustrating consistent lock ordering to prevent deadlock
Type a comment illustrating consistent lock ordering to prevent deadlock
Answer
// Both threads: acquire lockA first, then lockB synchronized (lockA) { synchronized (lockB) { /* safe */ } }
Consistent acquisition order means at most one thread can hold lockA at a time. The second thread blocks on lockA rather than holding lockB and waiting for lockA — deadlock cannot form.