-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLeftRight.java
More file actions
53 lines (50 loc) · 1.09 KB
/
Copy pathLeftRight.java
File metadata and controls
53 lines (50 loc) · 1.09 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
package java_base;
public class LeftRight {
boolean flag = false; //false 是左 ture 是右
class Left extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0; i < 50; i++)
synchronized (LeftRight.class) {
while(flag)
{
try {
LeftRight.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.printf("%30s\n", "左脚一步");
flag = true;
LeftRight.class.notify();
}
}
}
class Right extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0; i < 50; i++)
synchronized (LeftRight.class) {
while(!flag)
{
try {
LeftRight.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.printf("%30s\n", "右脚一步");
flag = false;
LeftRight.class.notify();
}
}
}
public static void main(String[] args) {
// TODO Auto -generated method stub
LeftRight lf = new LeftRight();
lf.new Left();
lf.new Right();
}
}