forked from cirosantilli/java-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeadlock.java
More file actions
125 lines (100 loc) · 3.36 KB
/
Deadlock.java
File metadata and controls
125 lines (100 loc) · 3.36 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* Sample code that very often leads ot a deadlock.
*
* When one bows, he cannot get up before the other bows back.
*
* The problem is when both bow at the same time: the cannot get up and bow
* back because the other hasn't bowed back yet! The result is that both threads to nothing.
*
* Furthermore, no one can bow and bow back at the same!
* This is all implied by the synchronized keyword:
* only one of the synchronized methods of an *OBJECT* can take place at a time.
*/
public class Deadlock {
static class Friend {
private final String name;
public Friend(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public synchronized void bow(Friend bower) {
System.out.format("%s: %s has bowed to me!%n",
this.name, bower.getName());
bower.bowBack(this);
}
/**
* This fails!
*
* Reason:
*
* void m() {
* //
* }
*
* is different than:
*
* void m() {
* synchronized (this) {
* //
* }
* }
*
* Because on the second, multiple methods on the same data *can* enter method `m()`,
* while on the first they cannot.
*
* This makes a big difference here, because if the method enters bowSyncFail(),
* it blocks other synchronized methods (`bowBack`), causing the deadlock.
*/
public synchronized void bowSyncFail(Friend bower) {
//System.out.println("before other bowed to me");
synchronized (BowLock.class) {
System.out.format("%s: %s has bowed to me!%n",
this.name, bower.getName());
bower.bowBack(this);
}
}
public synchronized void bowBack(Friend bower) {
System.out.format("%s: %s"
+ " has bowed back to me!%n",
this.name, bower.getName());
}
}
public static class BowLock {}
public static void main(String[] args) {
final Friend a = new Friend("a");
final Friend b = new Friend("b");
Thread ta;
Thread tb;
switch ( 0 ) {
case 0:
//bow
ta = new Thread(new Runnable() {
public void run() { a.bow(b); }
});
tb = new Thread(new Runnable() {
public void run() { b.bow(a); }
});
ta.start();
tb.start();
break;
case 1:
//bowSyncFail
ta = new Thread(new Runnable() {
public void run() { a.bowSyncFail(b); }
});
tb = new Thread(new Runnable() {
public void run() { b.bowSyncFail(a); }
});
ta.start();
tb.start();
break;
case 2:
//Here the only solution is for the first bow finish before the second thread enters it.
//But if the first thread finishes bow, it also finishes its operation!
//Therefore, the best solution in this case is to simply not paralellize.
break;
}
}
}