source:http://www.journaldev.com/1058/java-deadlock-example-and-how-to-analyze-deadlock-situation
Deadlock is a programming situation where two or more threads are blocked forever, this situation arises with at least two threads and two or more resources. Here I have written a simple program that will cause deadlock scenario and then we will see how to analyze it.
Java Deadlock Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
In above program SyncThread is implementing Runnable interface and it works on two Objects by acquiring lock on each one of them one by one using synchronized block.
In main method, I have three threads running for SyncThread and there is a shared resource between each of the threads. The threads are run in such a way that it will be able to acquire lock on the first object but when it’s trying to acquire lock on second object, it goes on wait state because it’s already locked by another thread. This forms a cyclic dependency for resource between Threads causing deadlock.
When I execute the above program, here is the output generated but program never terminates because of deadlock.
1 2 3 4 5 6 7 8 9 |
|
Here we can clearly identify the deadlock situation from the output but in real life applications it’s very hard to find the deadlock situation and debug them.
Analyze Deadlock
To analyze a deadlock, we need to look at the java thread dump of the application, in last post I explained how we can generate thread dump using VisualVM profiler or using jstack utility.
Here is the thread dump of above program.
|
The thread dump output clearly shows the deadlock situation and threads and resources involved causing deadlock situation.
For analyzing deadlock, we need to look out for the threads with state as BLOCKED and then the resources it’s waiting to lock, every resource has a unique ID using which we can find which thread is already holding the lock on the object. For example Thread “t3” is waiting to lock 0x000000013df2f658 but it’s already locked by thread “t1”.
Once we analyze the deadlock situation and found out the threads which are causing deadlock, we need to make code changes to avoid deadlock situation.
Avoid deadlock
These are some of the guidelines using which we can avoid most of the deadlock situations.
- Avoid Nested Locks: This is the most common reason for deadlocks, avoid locking another resource if you already hold one. It’s almost impossible to get deadlock situation if you are working with only one object lock. For example, here is the another implementation of run() method without nested lock and program runs successfully without deadlock situation.
12
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public
void
run() {
String name = Thread.currentThread().getName();
System.out.println(name +
" acquiring lock on "
+ obj1);
synchronized
(obj1) {
System.out.println(name +
" acquired lock on "
+ obj1);
work();
}
System.out.println(name +
" released lock on "
+ obj1);
System.out.println(name +
" acquiring lock on "
+ obj2);
synchronized
(obj2) {
System.out.println(name +
" acquired lock on "
+ obj2);
work();
}
System.out.println(name +
" released lock on "
+ obj2);
System.out.println(name +
" finished execution."
);
}
- Lock Only What is Required: You should acquire lock only on the resources you have to work on, for example in above program I am locking the complete Object resource but if we are only interested in one of it’s fields, then we should lock only that specific field not complete object.
- Avoid waiting indefinitely: You can get deadlock if two threads are waiting for each other to finish indefinitely using thread join. If your thread has to wait for another thread to finish, it’s always best to use join with maximum time you want to wait for thread to finish.
If you liked this tutorial, make sure to check out other java thread tutorial.