运行时只会出现“李四————12”这个结果,求指导出现问题的地方
class People{
private String name;
private int age;
private boolean flag = false;
public synchronized void set(String name,int age) {
if(flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name;
this.age = age;
flag = true;
this.notify();
}
public synchronized void out() {
if(!flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name+"————"+age);
flag = false;
this.notify();
}
}
class my_In implements Runnable{
People p1;
my_In(People p1){
this.p1 = p1;
}
public void run() {
int y = 0;
while(true) {
if(y==0) {
p1.set("李四", 12);
}else {
p1.set("张三", 21);
}
y = (y+1)%2;
// p1.set("李四", 12);
}
}
}
class my_Out implements Runnable{
People p1;
my_Out(People p1){
this.p1 = p1;
}
public void run() {
p1.out();
}
}
public class WNDemo {
public static void main(String[] args) {
People p = new People();
my_In m1 = new my_In(p);
my_Out m2 = new my_Out(p);
Thread t1 = new Thread(m1);
Thread t2 = new Thread(m2);
t1.start();
t2.start();
}
}