The 30 second answer
A deadlock is when two threads each hold a lock the other needs, so both wait forever. Starvation is when one thread rarely gets a resource because others keep taking it. Livelock is when threads keep reacting to each other but make no progress. A race condition is when the result depends on timing, like two threads incrementing the same counter. Fixes: consistent lock order, timeouts, and atomic or concurrent classes.
Classic deadlock and the fix:
// Thread 1: lock(a) then lock(b); Thread 2: lock(b) then lock(a) -> deadlock
void transfer(Account from, Account to, long amt) {
Account first = from.id() < to.id() ? from : to; // global lock order
Account second = first == from ? to : from;
synchronized (first) { synchronized (second) { from.debit(amt); to.credit(amt); } }
}Prevention:
- Deadlock: acquire locks in a fixed order; use
tryLockwith a timeout; don't call unknown code while holding a lock. - Starvation: keep critical sections short; consider fair locks.
- Livelock: add randomized backoff on retry.
- Race conditions: use atomics, concurrent collections, immutable objects, or a lock around check-then-act sequences.
Detection: jcmd <pid> Thread.print (or jstack) prints a thread dump and reports Java-level deadlocks; ThreadMXBean.findDeadlockedThreads() does it from code.
Alternate interview scenarios
"What is the difference between wait() and sleep()? Where would each cause a deadlock?"
wait() must be called while holding the object's monitor, and it releases that lock while waiting, so another thread can enter and call notify(). sleep() just pauses the thread, and the Javadoc says it does not lose ownership of any monitors. So sleep causes trouble when it's called inside a synchronized block: every thread that needs that lock is stuck for the whole sleep, and if the sleeping thread is waiting for one of them to change a flag, nothing moves. Wait causes trouble in a different way. A missed notify, where notify runs before the other thread starts waiting, leaves it waiting forever. And wait only releases the lock it is called on, so a thread holding two locks keeps the other one. The fixes: always wait in a loop on a condition, because wake-ups can be spurious; never sleep while holding a lock; and prefer java.util.concurrent tools such as CountDownLatch or a BlockingQueue.The safe wait pattern, and the same handoff with a latch:
private final Object lock = new Object();
private boolean ready;
void await() throws InterruptedException {
synchronized (lock) {
while (!ready) lock.wait(); // loop: re-check after every wake-up, spurious or not
}
}
void signal() {
synchronized (lock) { ready = true; lock.notifyAll(); }
}
// Same handoff with java.util.concurrent: no lost signal, no manual loop
CountDownLatch started = new CountDownLatch(1);
// worker thread: started.countDown();
started.await(); // returns even if countDown() ran first"How does a deadlock actually happen, and how do you resolve one in production?"
jcmd <pid> Thread.print prints a section that starts with "Found one Java-level deadlock" and names each thread and the monitor it is waiting for. Restarting the instance stops the outage, but the real fix is in the code: take locks in one global order (for example by account id), use tryLock with a timeout and back off, or shrink the critical section so you never hold one lock while calling code that takes another.Database deadlocks behave differently, and interviewers often mean those. PostgreSQL detects them and aborts one of the transactions, and which one it aborts is not predictable. So the application sees an error and must retry the whole transaction. The PostgreSQL docs give the same prevention rule as for Java locks: have every transaction lock rows in a consistent order, for example by updating accounts sorted by id.
Sources (9)
- Java Tutorial: Deadlockdocs.oracle.com
- Java Tutorial: Starvation and Livelockdocs.oracle.com
- Java Tutorial: Thread Interferencedocs.oracle.com
- jcmddocs.oracle.com
- ThreadMXBean Javadocdocs.oracle.com
- Object.wait Javadoc (Java 21)docs.oracle.com
- Thread.sleep Javadoc (Java 21)docs.oracle.com
- CountDownLatch Javadoc (Java 21)docs.oracle.com
- PostgreSQL 17 docs: Explicit locking, deadlockspostgresql.org