Quiz: Deadlock in Java: What It Is and How to Avoid It

Total: 6 questions

1. 

What is a deadlock in Java?

A deadlock is a situation where two or more threads wait for each other forever: each one holds a resource the other needs, so none of them can continue. In Java, it usually happens when threads acquire synchronized monitors in different orders: one takes A, then B, the other takes B, then A.

A deadlock throws no exception and the CPU stays idle - the program simply stops responding. The JVM does not resolve a deadlock on its own, and a thread in the BLOCKED state ignores interrupt(), so with synchronized locks the only way out is a restart. Deadlocks need to be prevented, not cured.

2. 

What are the 4 Coffman conditions for a deadlock?

A deadlock can occur only when all four conditions hold at the same time (Edward Coffman, 1971):

  • Mutual exclusion - only one thread can hold a monitor.
  • Hold and wait - a thread holds one resource and waits for another.
  • No preemption - a monitor can't be taken away; it is released only when the thread leaves synchronized.
  • Circular wait - A waits for B, B waits for A (or A → B → C → A).

Breaking any one condition is enough. The easiest one to break is circular wait: use the same lock order in every thread.

3. 

How do you avoid a deadlock in Java?

The main technique is a consistent lock order in every thread: then a circular wait can't form. If the monitors come in as parameters (for example, transfer(from, to)), order them by a stable key, such as the account id.

Other things that help:

  • tryLock(timeout) on a ReentrantLock: if the thread can't get the second resource in time, it releases the first one and retries after a random pause;
  • as little code as possible inside synchronized, and no calls to "foreign" code (listeners, callbacks) while holding a lock;
  • no waiting inside synchronized with sleep(), join() or CountDownLatch.await() - none of them releases the monitor.
4. 

How do you detect a deadlock in a running Java program?

Take a thread dump: find the PID with jps -l, then run jstack PID or jcmd PID Thread.print. The JVM finds cycles on monitors itself and prints "Found one Java-level deadlock" at the end of the dump, with the names of the threads and the objects they are waiting for.

Graphical options: the "Detect Deadlock" button in JConsole, the Threads tab in VisualVM (downloaded separately since JDK 9). From code: ManagementFactory.getThreadMXBean().findDeadlockedThreads() returns the thread ids or null and sees both synchronized and ReentrantLock (the older findMonitorDeadlockedThreads() sees monitors only). Meaningful thread names make the dump much easier to read.

5. 

What is the difference between deadlock, livelock and starvation?

  • Deadlock - threads are blocked (BLOCKED/WAITING) and do nothing, CPU usage is near zero, and jstack shows "Found one Java-level deadlock". Fixed with a consistent lock order or tryLock.
  • Livelock - threads keep running (RUNNABLE, high CPU) but endlessly yield to each other and make no progress, like two people in a corridor who keep stepping to the same side. Fixed with a random pause before retrying.
  • Starvation - one thread can't get a resource for a long time while the others work normally. Holding monitors briefly and a fair lock, new ReentrantLock(true), help.
6. 

What is the difference between a deadlock and a race condition?

A race condition means too little synchronization: threads modify shared data at the same time and the result is wrong, but the program keeps running. A deadlock means the synchronization is wrong: the data stays intact, but the program stops forever.

When you fix a race condition with synchronized, it's easy to create a deadlock if different threads acquire several monitors in different orders - so keep an eye on the lock order.

Page 1 of 1