~/ learn/ java-from-zero/ cards/ Deadlock, livelock & safe design strategies
1 of 4

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.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/bf4a596f-f0da-4a21-bcba-f2e1f61f1721/flashcard utf-8 LF