写的等待唤醒机制为什么什么也没输出?不解....
package com.thread;
public class DoubleRunnable {
public static void main(String[] args) {
Person p = new Person();
new Thread(new Input(p)).start();
new Thread(new OutPut(p)).start();
}
}
class Input implements Runnable
{
private Person p;
Input(Person p){
this.p=p;
}
public void run(){
boolean b = true;
while(true){
if(b=true){
p.set("zhangsan", "nan");
b=false;
}else{
p.set("小小", "女");
b=true;
}
}
}
}
class OutPut implements Runnable
{
private Person p;
OutPut(Person p){
this.p=p;
}
public void run(){
while(true){
p.out();
}
}
}
class Person{
private String name;
private String sex;
private boolean flag=false;
public synchronized void set(String name,String sex){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
this.name=name;
this.sex=sex;
flag=true;
this.notify();
}
}
public synchronized void out(){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name+"=="+sex);
flag=false;
this.notify();
}
}
}
|