class res
{
String name ;
String sex;
}
class input implements Runnable
{
private res r;
input(res r)
{
this.r = r ;
}
public void run()
{
int x=0;
while(true)
{
synchronized(r)
{
if (x==0)
{
r.name="zhang ";
r.sex="man";
}
else
{
r.name="丽丽";
r.sex="女";
}
x=(x+1)%2;
}
}
}
}
class output implements Runnable
{
private res r;
output(res r)
{
this.r = r ;
}
public void run()
{
while(true)
{
synchronized(r)
{
System.out.println(r.name+"...."+r.sex);
}
}
}
}
class inputoutputdome
{
public static void main(String[] args)
{
res r =new res();
input in = new input(r);
output ou = new output(r);
Thread t1 =new Thread(in);
Thread t2 =new Thread(ou);
t1.start();
t2.start();
}
}
打印出现 null...null后后面就正常了!
代码中还有问题!求解?
|
|