0
0
JavaConceptBeginner · 3 min read

What is Deadlock in Java: Explanation and Example

In Java, deadlock is a situation where two or more threads are blocked forever, each waiting for the other to release a resource. This happens when threads hold locks and wait for locks held by each other, causing a standstill.
⚙️

How It Works

Imagine two people each holding a key to a different door, but each needs the other's key to open their own door. Neither can proceed because they are waiting for the other to give up their key. This is similar to deadlock in Java threads.

In Java, threads often need to lock resources to work safely. If Thread A locks Resource 1 and waits for Resource 2, while Thread B locks Resource 2 and waits for Resource 1, both threads get stuck waiting forever. This is called deadlock.

Deadlock happens when four conditions occur together: mutual exclusion (only one thread can hold a resource), hold and wait (threads hold resources while waiting for others), no preemption (resources cannot be forcibly taken), and circular wait (a cycle of threads waiting on each other).

💻

Example

This example shows two threads causing a deadlock by each trying to lock two objects in opposite order.

java
public class DeadlockExample {
    private static final Object resource1 = new Object();
    private static final Object resource2 = new Object();

    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            synchronized (resource1) {
                System.out.println("Thread 1: Locked resource 1");
                try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
                synchronized (resource2) {
                    System.out.println("Thread 1: Locked resource 2");
                }
            }
        });

        Thread thread2 = new Thread(() -> {
            synchronized (resource2) {
                System.out.println("Thread 2: Locked resource 2");
                try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
                synchronized (resource1) {
                    System.out.println("Thread 2: Locked resource 1");
                }
            }
        });

        thread1.start();
        thread2.start();
    }
}
Output
Thread 1: Locked resource 1 Thread 2: Locked resource 2
🎯

When to Use

Deadlock is not something you want to use; it is a problem to avoid in Java programs that use multiple threads. Understanding deadlock helps you write safer code when threads share resources.

Deadlocks often happen in complex systems like databases, web servers, or any program where many threads access shared data. Knowing about deadlock helps you design your program to prevent it by careful resource management or using higher-level concurrency tools.

Key Points

  • Deadlock occurs when threads wait forever for each other’s locked resources.
  • It requires four conditions: mutual exclusion, hold and wait, no preemption, and circular wait.
  • Deadlock causes programs to freeze and stop making progress.
  • Prevent deadlock by avoiding circular waits or using timeout locks.

Key Takeaways

Deadlock in Java happens when threads wait forever for each other’s locked resources.
It occurs due to mutual exclusion, hold and wait, no preemption, and circular wait conditions.
Deadlocks cause programs to freeze and must be avoided for smooth concurrency.
Use careful resource ordering or timeout locks to prevent deadlocks.
Understanding deadlock helps write safer multithreaded Java programs.