package Res;
class ResDemo
{
public static void main(String[] args)
{
Res r = new Res();
Input i = new Input(r);
Output o = new Output(r);
Thread t1 = new Thread(i);
Thread t2 = new Thread(o);
t1.start();
t2.start();
}
}
class Res
{
boolean flag = false;
String name;
String sex;
}
class Input implements Runnable
{
private Res r;
Input(Res r)
{
this.r = r;
}
public void run()
{
int num = 0;
while(true)
{
while(r.flag)
try{wait();}catch(Exception e){}
num = ++num % 2;
if(num == 0)
{
r.name = "张三";
r.sex = "男";
}
else
{
r.name = "LiXue";
r.sex = "girl";
}
r.flag = true;
//try{notify();}catch(Exception e){}
}
}
}
class Output implements Runnable
{
private Res r;
Output(Res r)
{
this.r = r;
}
public void run()
{
while(true)
{
while(!r.flag)
try{wait();}catch(Exception e){}
System.out.println(r.name+"...... "+r.sex);
r.flag = false;
//try{notify();}catch(Exception e){}
}
}
}
打印结果是什么?我不理解结果 |
|