黑马程序员技术交流社区
标题:
高手教教我
[打印本页]
作者:
易东俊
时间:
2014-10-21 21:09
标题:
高手教教我
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){}
}
}
}
打印结果是什么?我不理解结果
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2