C
C
Core Java
Search…
Deadlock
Imagine a tester and a developer are working on two applications, an Android application and an iPhone application. The Android application is in its testing phase; it should be tested and the reported bugs should be fixed by the developer. The iPhone application needs to be developed from scratch; it should be coded by the developer and then tested for bugs. Imagine the tester starts testing the Android application and the developer starts working with the iPhone application. The Android application completes the testing phase and requests for the developer to fix the bugs. At the same time, the iPhone application completes its development phase and requests the tester to test it. The managers working with both the Android and iPhone applications refuse to release their resources (developer or tester) before their project completes. So both projects are waiting for the other project to complete—that is, waiting for the same set of resources. This causes a deadlock—these threads might wait forever. Here’s how this looks in code:

Code Snippet :

1
package com.gs.corejava.collections;
2
3
class Developer {
4
5
public synchronized void fixBugs() {
6
System.out.println("Fixing bugs..");
7
}
8
9
public synchronized void code() {
10
System.out.println("Coding..");
11
}
12
}
13
14
class Tester {
15
public synchronized void testAppIn() {
16
System.out.println("Testing..");
17
}
18
}
19
20
/**
21
* Already developed , Hence first testing then fix bugs by dev team
22
*
23
* @author momalhotra
24
*
25
*/
26
class AndroidApp implements Runnable {
27
Developer developer;
28
Tester tester;
29
30
/**
31
* @param developer
32
* @param tester
33
*/
34
public AndroidApp(Developer developer, Tester tester) {
35
super();
36
this.developer = developer;
37
this.tester = tester;
38
}
39
40
public void run() {
41
synchronized (tester) {
42
tester.testAppIn();
43
developer.fixBugs();
44
}
45
}
46
47
}
48
49
/**
50
* Starting development , then will be given to testing team
51
*
52
* @author momalhotra
53
*
54
*/
55
class IphoneApp implements Runnable {
56
Developer developer;
57
Tester tester;
58
59
/**
60
* @param developer
61
* @param tester
62
*/
63
public IphoneApp(Developer developer, Tester tester) {
64
super();
65
this.developer = developer;
66
this.tester = tester;
67
}
68
69
public void run() {
70
synchronized (developer) {
71
developer.code();
72
tester.testAppIn();
73
}
74
}
75
76
}
77
78
public class Deadlock {
79
public static void main(String[] args) {
80
Developer mohit = new Developer();
81
Tester neeti = new Tester();
82
83
Thread iphoneApp = new Thread(new IphoneApp(mohit, neeti));
84
Thread androidApp = new Thread(new AndroidApp(mohit, neeti));
85
86
iphoneApp.start();
87
androidApp.start();
88
}
89
}
90
Copied!

Database Deadlocks

A more complicated situation in which deadlocks can occur, is a database transaction. A database transaction may consist of many SQL update requests. When a record is updated during a transaction, that record is locked for updates from other transactions, until the first transaction completes. Each update request within the same transaction may therefore lock some records in the database.
If multiple transactions are running at the same time that need to update the same records, there is a risk of them ending up in a deadlock.
For example
1
Transaction 1, request 1, locks record 1 for update
2
Transaction 2, request 1, locks record 2 for update
3
Transaction 1, request 2, tries to lock record 2 for update.
4
Transaction 2, request 2, tries to lock record 1 for update.
Copied!
Since the locks are taken in different requests, and not all locks needed for a given transaction are known ahead of time, it is hard to detect or prevent deadlocks in database transactions.

Examples:

1)

1
package com.gs.corejava.collections;
2
3
public class Deadlock1 {
4
5
public static void main(String[] args) {
6
try {
7
Thread.currentThread().join();
8
} catch (InterruptedException e) {
9
e.printStackTrace();
10
}
11
}
12
}
Copied!

2)

1
package com.gs.corejava.collections;
2
3
public class Deadlock2 {
4
5
static String s1 = "Mohit";
6
static String s2 = "Neeti";
7
8
public static void main(String[] args) {
9
10
new Thread() {
11
public void run() {
12
synchronized (s1) {
13
try {
14
Thread.sleep(100);
15
} catch (InterruptedException e) {
16
e.printStackTrace();
17
}
18
synchronized (s2) {
19
20
}
21
}
22
}
23
}.start();
24
25
new Thread() {
26
public void run() {
27
synchronized (s2) {
28
synchronized (s1) {
29
30
}
31
}
32
}
33
}.start();
34
35
}
36
}
Copied!
Programmatically, You can detect the threads which have entered into deadlock condition and also you can retrieve the details about them. This can be done using ThreadMXBean interface of java.lang.Management package. You can go through the oracle docs of ThreadMXBean interface.
First, you have to get an instance of ThreadMXBean using getThreadMXBean() method of ManagementFactory, like this.
1
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
After getting an instance of ThreadMXBean, call findMonitorDeadlockedThreads() method on it. It returns an array of type long containing ids of all currently deadlocked threads.
1
long ids[] = bean.findMonitorDeadlockedThreads();
After getting the ids of deadlocked threads, pass these ids to getThreadInfo() method of ThreadMXBean. It will return an array of ThreadInfo objects, where one ThreadInfo object contains the details of one deadlocked thread.
1
ThreadInfo threadInfo[] = bean.getThreadInfo(ids);
Iterate the ThreadInfo array to get the details of individual deadlocked thread.
for (ThreadInfo threadInfo1 : threadInfo){ System.out.println(threadInfo1.getThreadName()); //Prints the name of deadlocked thread

Example :

1)

1
package com.gs.corejava.collections;
2
3
import java.lang.management.ManagementFactory;
4
import java.lang.management.ThreadInfo;
5
import java.lang.management.ThreadMXBean;
6
7
public class Deadlock2 {
8
9
static String s1 = "Mohit";
10
static String s2 = "Neeti";
11
12
public static void main(String[] args) {
13
14
new Thread() {
15
public void run() {
16
synchronized (s1) {
17
try {
18
Thread.sleep(100);
19
} catch (InterruptedException e) {
20
e.printStackTrace();
21
}
22
synchronized (s2) {
23
24
}
25
}
26
}
27
}.start();
28
29
new Thread() {
30
public void run() {
31
synchronized (s2) {
32
synchronized (s1) {
33
34
}
35
}
36
}
37
}.start();
38
39
try {
40
Thread.sleep(5000);
41
} catch (InterruptedException e) {
42
e.printStackTrace();
43
}
44
45
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
46
long[] ids = threadMXBean.findMonitorDeadlockedThreads();
47
ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(ids);
48
49
for (ThreadInfo info : threadInfos) {
50
System.out.println(info);
51
}
52
}
53
}
Copied!

2)

1
public class Deadlock {
2
public static void main(String[] args) {
3
Developer mohit = new Developer();
4
Tester neeti = new Tester();
5
6
Thread iphoneApp = new Thread(new IphoneApp(mohit, neeti));
7
Thread androidApp = new Thread(new AndroidApp(mohit, neeti));
8
9
iphoneApp.setName("Iphone App maker thread");
10
androidApp.setName("Android App maker thread");
11
iphoneApp.start();
12
androidApp.start();
13
14
try {
15
Thread.sleep(5000);
16
} catch (InterruptedException e) {
17
e.printStackTrace();
18
}
19
20
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
21
long[] ids = threadMXBean.findMonitorDeadlockedThreads();
22
ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(ids);
23
24
for (ThreadInfo info : threadInfos) {
25
System.out.println(info);
26
}
27
}
28
}
Copied!
Last modified 2yr ago