class res
{
String name;
String sex;
boolean flag=false;这个flag 是默认错误
}
classIntput implements Runnable
private Res r;
Intput(Res r)
{
this.r=r;
synchronized(r)
{
int x=0
while(true)
{
if(r.flag) ;;;;这里怎么是ture
wait();
if (x==0)
{
r.name="mike";
r.sex="man";
}
else
{
r.name="莉莉";
r.sex="女";
}
x=(x+1)%2
r.flag=true;
notify();
}
}
}
classOutput implements Runnable
{
private Res r;
Output(Res r)
{
this.r=r;
}
public void run()
{
while(true)
{
synchronized(r)
{
if(!r.flag)
wait();
System.out.println(r.name+"...."+r.sex);
r.flag=false;
notify();
}
}
}
}
class InputOutputDemo
{
public static void main(String[] args)
{
Res r=new Res();
Intput=new Intput(r);
Output=new Output(r);
Thread t1=new Thread(in);
Thread t1=new Thread(out);
t1.start();
t2.start();
}
|
|