class Res
{
String name;
String sex;
}
class Input implements Runnable
{
private Res r;
Object obj=new Object();
Input(Res r)
{
this.r=r;
}
public void run()
{
int x=0;
while(true)
{ synchronized(obj)
{
if(r.flag)
wait();
if (x==0)
{
r.name="Mike";
r.sex="man";
}
else
{
r.name="丽丽";
r.sex="女女女女女";
}
x=(x+1)%2;
r.flag=true;
notify();
}
}
}
}
class Output implements Runnable
{
private Res r;
Object obj=new Object();
Output(Res r)
{
this.r=r;
}
public void run()
{
while(true)
{
synchronized(obj)
{
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();
Input in=new Input(r);
Output out=new Output(r);
Thread t1=new Thread(in);
Thread t2=new Thread(out);
t1.start();
t2.start();
}
}
|