forked from liujiboy/Java_Course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest5.java
More file actions
49 lines (43 loc) · 889 Bytes
/
Test5.java
File metadata and controls
49 lines (43 loc) · 889 Bytes
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
package cqu;
public class Test5 {
public static void main(String[] args) {
final Object lockT1 = new Object();
final Object lockT2 = new Object();
Thread t1 = new Thread() {
public void run() {
for (int i = 0; i < 10; i++) {
synchronized (lockT2) {
synchronized (lockT1) {
System.out.print("t1");
lockT1.notify();
}
try {
lockT2.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
Thread t2 = new Thread() {
public void run() {
for (int i = 0; i < 10; i++) {
synchronized (lockT1) {
synchronized (lockT2) {
System.out.print("t2");
lockT2.notify();
}
try {
lockT1.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
t1.start();
t2.start();
}
}