public void run(){
int x = 0 ,y = 1;
while(y++<=50000){
synchronized(r){
if(r.flag)
try{r.wait();}
catch(Exception e){}
if(x==0){
r.name = "Tom";
r.sex = "Man";
}
else{
r.name = "丽丽";
r.sex = "女孩女孩";
}
}
x = (x+1)%2;
r.flag = true;
r.notify();
}
标红色的代码也应该在同步内,不然的话就会报你那个异常,因为唤醒的时候不早知道是哪个线程 |