Deadlock in Java – How to Detect & Reduce Java Deadlock
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
1. Deadlock in Java – Objective
After studying constructor chaining in Java, we are ready to discuss what is a deadlock in Java multithreading. Moreover, we will discuss how to detect a deadlock condition in Java. At last, we will discuss how to reduce deadlock in Java multithreading.
So, let’s start Java Deadlock Tutorial.
2. What is Deadlock in Java?
Java Keywords that are synchronized are to make class thread-safe which means only one of the method has the lock of the synchronized method which can use it.
If the program that we are operating is running in a multi-threaded environment where two or more than two of the threads are working together. But this is not mandatory to work all the time this type of problem is called Deadlock in Java.
Do you know What is Java Semaphore?
Deadlock Example in Java-
class Util { static void sleep(long millis) { try { Thread.sleep(millis); } catch (InterruptedException e) { e.printStackTrace(); } } } class Shared { System.out.println("test1-begin"); Util.sleep(1000); s2.test2(this); System.out.println("test1-end"); } synchronized void test2(Shared s1) { System.out.println("test2-begin"); Util.sleep(1000); s1.test1(this); System.out.println("test2-end"); } } class Thread1 extends Thread { private Shared s1; private Shared s2; public Thread1(Shared s1, Shared s2) { this.s1 = s1; this.s2 = s2; } @Override public void run() { s1.test1(s2); } } class Thread2 extends Thread { private Shared s1; private Shared s2; public Thread2(Shared s1, Shared s2) { this.s1 = s1; this.s2 = s2; } @Override public void run() { s2.test2(s1); } } public class GFG { public static void main(String[] args) { Shared s1 = new Shared(); Shared s2 = new Shared(); Thread1 t1 = new Thread1(s1, s2); t1.start(); Thread2 t2 = new Thread2(s1, s2); t2.start(); Util.sleep(2000); } }
No one is ever recommended to run this program with online IDE. The alternate technique can be to copy the whole article and then run it on local machines. This can be seen that it runs on indefinite time because of the reason these threads are in a position of deadlock which does no led the codes to be executed.
Read about Types of Exceptions in Java Programming
a. How to Detect Deadlock in Java
There is one more method to detect Deadlock in Java, it can be done by running the program in CMD. All we need to do is collect thread dumps and then we have to command to collect, depending upon the operating system. If we are running Java 8 on windows, a command would be jcmd $PID Thread.print.
5524:
2017-04-21 09:57:39
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.25-b02 mixed mode):
“DestroyJavaVM” #12 prio=5 os_prio=0 tid=0x0000000002690800 nid=0xba8 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
“Thread-1” #11 prio=5 os_prio=0 tid=0x0000000018bbf800 nid=0x12bc waiting for monitor entry [0x000000001937f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at Shared.test1(GFG.java:15)
– waiting to lock (a Shared)
at Shared.test2(GFG.java:29)
– locked (a Shared)
at Thread2.run(GFG.java:68)
“Thread-0” #10 prio=5 os_prio=0 tid=0x0000000018bbc000 nid=0x1d8 waiting for monitor entry [0x000000001927f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at Shared.test2(GFG.java:25)
– waiting to lock (a Shared)
at Shared.test1(GFG.java:19)
– locked (a Shared)
at Thread1.run(GFG.java:49)
“Service Thread” #9 daemon prio=9 os_prio=0 tid=0x000000001737d800 nid=0x1680 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
“C1 CompilerThread2” #8 daemon prio=9 os_prio=2 tid=0x000000001732b800 nid=0x17b0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
“C2 CompilerThread1” #7 daemon prio=9 os_prio=2 tid=0x0000000017320800 nid=0x7b4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
“C2 CompilerThread0” #6 daemon prio=9 os_prio=2 tid=0x000000001731b000 nid=0x21b0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
“Attach Listener” #5 daemon prio=5 os_prio=2 tid=0x0000000017319800 nid=0x1294 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
“Signal Dispatcher” #4 daemon prio=9 os_prio=2 tid=0x0000000017318000 nid=0x1efc runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
“Finalizer” #3 daemon prio=8 os_prio=1 tid=0x0000000002781800 nid=0x5a0 in Object.wait() [0x000000001867f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
– waiting on (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
– locked (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
at java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)
“Reference Handler” #2 daemon prio=10 os_prio=2 tid=0x000000000277a800 nid=0x15b4 in Object.wait() [0x000000001857f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
– waiting on (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Unknown Source)
at java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)
– locked (a java.lang.ref.Reference$Lock)
“VM Thread” os_prio=2 tid=0x00000000172e6000 nid=0x1fec runnable
“GC task thread#0 (ParallelGC)” os_prio=0 tid=0x00000000026a6000 nid=0x21fc runnable
“GC task thread#1 (ParallelGC)” os_prio=0 tid=0x00000000026a7800 nid=0x2110 runnable
“GC task thread#2 (ParallelGC)” os_prio=0 tid=0x00000000026a9000 nid=0xc54 runnable
“GC task thread#3 (ParallelGC)” os_prio=0 tid=0x00000000026ab800 nid=0x704 runnable
“VM Periodic Task Thread” os_prio=2 tid=0x0000000018ba0800 nid=0x610 waiting on condition
JNI global references: 6
Found one Java-level deadlock:
=============================
“Thread-1”:
waiting to lock monitor 0x0000000018bc1e88 (object 0x00000000d5d645a0, a Shared),
which is held by “Thread-0”
“Thread-0”:
waiting to lock monitor 0x0000000002780e88 (object 0x00000000d5d645b0, a Shared),
which is held by “Thread-1”
Java stack information for the threads listed above:
===================================================
“Thread-1”:
at Shared.test1(GFG.java:15)
– waiting to lock (a Shared)
at Shared.test2(GFG.java:29)
– locked (a Shared)
at Thread2.run(GFG.java:68)
“Thread-0”:
at Shared.test2(GFG.java:25)
– waiting to lock (a Shared)
at Shared.test1(GFG.java:19)
– locked (a Shared)
at Thread1.run(GFG.java:49)
Found 1 deadlock.
The message displayed in end can happen to appear on your screens too.
Do you Know 7 Unknown Facts about Null in Java
b. How to Reduce Deadlock in Java
The occurrence of Deadlock in Java can be avoided by knowing its possibilities. it is really hard to catch one but still possible. The possibility of Java Deadlock cannot be destroyed but it can be reduced.
- Avoid Nested Locks-Â This is one of the main reasons for Deadlock in Java. Deadlock appears mainly when we give locks to multiple threads.
- Avoid Unnecessary Locks- Locks should only be provided to those which are really required unnecessary lock must be avoided.
- Using Thread Join-Â Position of Deadlock in Java appears when one of the thread is waiting for the other one to complete. If this situation occurs at that time we use Thread.join with the maximum number of times you think this execution will take.
So, this was all about Deadlock in Java Tutorial. Hope you like our explanation.
3. Conclusion
Hence, we studied about Deadlock in Java and how to detect Java deadlock conditions. In addition, we talked about how to reduce deadlock in Java with some Deadlock example. In our next tutorial, we will study Semaphore in Java. Till, if you have a query, feel free to ask in the comment box.
Related Articles-Â Â Java Image ProcessingÂ
Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google