~/ 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/09b46749-f44e-4ab0-bd8c-8621159f41b7/flashcard utf-8 LF