Skip to content
Veltoryx Technologies
ProLesson 9.11Advanced3 min readUpdated 30 October 2026

Deadlock, livelock and starvation

Recognise the three liveness problems, the four conditions behind every deadlock, and practical ways to prevent and diagnose them.

Race conditions give wrong results. Liveness problems make programs stop making progress. They're among the hardest bugs to reproduce, and favourite senior-level interview topics.

01Deadlock

Two threads each hold one lock and wait forever for the other's:

text
Thread A: holds lock(account1), waits for lock(account2)
Thread B: holds lock(account2), waits for lock(account1)
          → neither can ever continue

A typical cause is transferring money between two accounts while locking them in different orders:

java
void transfer(Account from, Account to, double amount) {
    synchronized (from) {
        synchronized (to) {          // A: lock(a1) then lock(a2); B: lock(a2) then lock(a1)
            from.debit(amount);
            to.credit(amount);
        }
    }
}
// Thread A: transfer(a1, a2, 100)    Thread B: transfer(a2, a1, 50)   → possible deadlock

Loading the rest of the lesson…