-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreadTest4.java
More file actions
53 lines (45 loc) · 1.31 KB
/
ThreadTest4.java
File metadata and controls
53 lines (45 loc) · 1.31 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
package thread;
/**
* Created by Administrator on 2017/5/5 0005.
*/
public class ThreadTest4 extends Thread{
private String name;
private Object prev;
private Object self;
public ThreadTest4(String name, Object prev, Object self) {
this.name = name;
this.prev = prev;
this.self = self;
}
public void run(){
int count = 10;
while (count>=0){
synchronized (prev){
synchronized (self){
System.out.print(name);
count--;
self.notify();
}
try {
prev.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws Exception {
Object a = new Object();
Object b = new Object();
Object c = new Object();
ThreadTest4 pa = new ThreadTest4("A", c, a);
ThreadTest4 pb = new ThreadTest4("B", a, b);
ThreadTest4 pc = new ThreadTest4("C", b, c);
new Thread(pa).start();
Thread.sleep(100); //确保按顺序A、B、C执行
new Thread(pb).start();
Thread.sleep(100);
new Thread(pc).start();
Thread.sleep(100);
}
}