-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLockTest_4.java
140 lines (136 loc) · 5.99 KB
/
LockTest_4.java
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//4 p; 4 r; 121 241 311 431 110 220 330 440
import java.util.Date;
public class LockTest_4 {
public static String obj1 = "resource 1";
public static String obj2 = "resource 2";
public static String obj3 = "resource 3";
public static String obj4 = "resource 4";
public static LA_check state_check_4 = new LA_check(4);
public static long startTime = System.currentTimeMillis();
public static void main(String[] args) {
LockA_4 la = new LockA_4();
new Thread(la).start();
LockB_4 lb = new LockB_4();
new Thread(lb).start();
LockC_4 lc = new LockC_4();
new Thread(lc).start();
LockD_4 ld = new LockD_4();
new Thread(ld).start();
}
}
class LockA_4 implements Runnable{
public void run() {
Thread.currentThread().setName("Process A");
try {
System.out.println(new Date().toString() + " -> " + Thread.currentThread().getName() + " starts...");
while(true){
synchronized (LockTest_4.obj2) {
System.out.println(new Date().toString() + " -> " + Thread.currentThread().getName() + " locked resource 2");
Thread.sleep(2000L);
System.out.println(new Date().toString() + " -> " + Thread.currentThread().getName() + " trying to get resource 1");
//110
if(!LockTest_4.state_check_4.detectDeadlock(3, 1))//p3 held the r1 which p1 is asking for
{
long endTime = System.currentTimeMillis();
System.out.println("running time: " + (endTime - LockTest_4.startTime) + "ms");
}
synchronized (LockTest_4.obj4) {
System.out.println("if you see this line, you fail");
System.out.println(new Date().toString() + " -> " + Thread.currentThread().getName() + " locked resource 1");
Thread.sleep(2000L);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class LockB_4 implements Runnable{
public void run() {
Thread.currentThread().setName("Process B");
try {
System.out.println(new Date().toString() + " -> " + Thread.currentThread().getName() + " starts...");
while(true){
synchronized (LockTest_4.obj4) {
System.out.println(new Date().toString() + " -> " + Thread.currentThread().getName() + " locked resource 4");
Thread.sleep(2000L);
System.out.println(new Date().toString() + " -> " + Thread.currentThread().getName() + " trying to get resource 2");
//220
Thread.sleep(50);
if(!LockTest_4.state_check_4.detectDeadlock(1, 2))
{
long endTime = System.currentTimeMillis();
System.out.println("running time: " + (endTime - LockTest_4.startTime) + "ms");
}
//p1 held the r2 which p2 is asking for
synchronized (LockTest_4.obj2) {
System.out.println("if you see this line, you fail");
System.out.println(new Date().toString() + " -> " + Thread.currentThread().getName() + " locked resource 2");
Thread.sleep(2000L);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class LockC_4 implements Runnable{
public void run() {
Thread.currentThread().setName("Process C");
try {
System.out.println(new Date().toString() + " -> " + Thread.currentThread().getName() + " starts...");
while(true){
synchronized (LockTest_4.obj1) {
System.out.println(new Date().toString() + " -> " + Thread.currentThread().getName() + " locked resource 1");
Thread.sleep(2000L);
System.out.println(new Date().toString() + " -> " + Thread.currentThread().getName() + " trying to get resource 3");
//330
Thread.sleep(100);
if(!LockTest_4.state_check_4.detectDeadlock(4, 3))//p4 held the r3 which p3 is asking for
{
long endTime = System.currentTimeMillis();
System.out.println("running time: " + (endTime - LockTest_4.startTime) + "ms");
}
synchronized (LockTest_4.obj3) {
System.out.println("if you see this line, you fail");
System.out.println(new Date().toString() + " -> " + Thread.currentThread().getName() + " locked resource 3");
Thread.sleep(2000L);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class LockD_4 implements Runnable{
public void run() {
Thread.currentThread().setName("Process D");
try {
System.out.println(new Date().toString() + " -> " + Thread.currentThread().getName() + " starts...");
while(true){
synchronized (LockTest_4.obj3) {
System.out.println(new Date().toString() + " -> " + Thread.currentThread().getName() + " locked resource 3");
Thread.sleep(2000L);
System.out.println(new Date().toString() + " -> " + Thread.currentThread().getName() + " trying to get resource 4");
//440
Thread.sleep(200);
if(!LockTest_4.state_check_4.detectDeadlock(2,4))//p2 held the r4 which p4 is asking for
{
long endTime = System.currentTimeMillis();
System.out.println("running time: " + (endTime - LockTest_4.startTime) + "ms");
}
synchronized (LockTest_4.obj4) {
System.out.println("if you see this line, you fail");
System.out.println(new Date().toString() + " -> " + Thread.currentThread().getName() + " locked resource 4");
Thread.sleep(2000L);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}