VOOZH about

URL: https://www.geeksforgeeks.org/java/deadlock-in-java-multithreading/

⇱ Deadlock in Java Multithreading - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Deadlock in Java Multithreading

Last Updated : 17 Dec, 2025

Deadlock is a situation in multithreading where two or more threads are permanently blocked because each one is waiting for the other to release a required lock. In simple terms, threads get stuck forever, and the program never continues.

  • Each thread holds a lock and waits for another lock held by a different thread.
  • This creates a circular wait, causing the application to freeze indefinitely.

Example: Below is a simple example demonstrating a deadlock condition in Java.

Output:

👁 Output
Output

Note: It is not recommended to run the program in an online IDE. We can run the above source code locally, but it gets stuck in a deadlock, preventing execution.

Explanation:

  • Thread t1 starts by acquiring a lock on the s1 and enters the test1() method of s1.
  • Thread t2 starts by acquiring a lock on the s2 and enters the test1() method of s2.
  • In the test1() method both threads try to acquire locks on each other's objects but the locks are already held by the other thread causing both threads to wait indefinitely for the other to release the lock.
  • Neither test1() nor test2() methods complete execution and the program remains stuck in the deadlock state.

Locks in Java

In Java, locks are mechanisms used to control access to shared resources in a multithreaded environment. Below is the diagrammatic representation of how Locks work and prevent Deadlock conditions.

👁 thread
Locks in Java

Detecting Deadlocks

We can detect deadlocks in a running Java program using the following steps:

1. List the active Java processes:

jps -l

Response:

👁 List of Running Thread
Response

This will list the running Java processes and also mention that there is a deadlock if we want to generate a thread dump.

2. Identify the process ID (PID) of the target program and run:

jcmd <PID> Thread.print // replace PID with the process ID

Replace <PID> with the process ID from the list provided by jps -l. This command outputs the state of the threads, which you can then analyze for deadlocks.

After running the above two commands, we can see deadlock occurs:

As we can see it is mentioned that found 1 deadlock.

Preventing Deadlocks

We can avoid deadlock conditions by knowing its possibilities. It's a very complex process and not easy to catch. Still, if we try, we can avoid this. There are some methods by which we can avoid this condition. We can't completely remove its possibility but we can reduce it.

  • Avoid Nested Locks: This is the main reason for deadlock. Mainly happens when we give locks to multiple threads. Avoid giving lock to multiple threads if we already have given to one.
  • Avoid Unnecessary Locks: We should have lock only those members who are required. Having a lock on unnecessarily can lead to deadlock.
  • Using thread join: Deadlock condition appears when one thread is waiting for the other to finish. If this condition occurs we can use Thread. Join the with the maximum time you think the execution will take.
Comment
Article Tags: