public class Student {
private boolean flag;
private String name;
private int age;
public synchronized void set(String name, int age) {
if (this.flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 此处不能加else语句,why?????
this.name = name;
this.age = age;
this.flag = true;
this.notify();
}
public synchronized void get() {
if (!this.flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//此处可以加else.why???????
else {
System.out.println(name + age);
this.flag = false;
this.notify();
}
}
}
第一个地方加else就不能实现 读一个数据,写一个数据. 第二个else加与不加不影响结果.why?
|
|