A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张玉建 中级黑马   /  2012-4-25 19:08  /  1870 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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后后面就正常了!
代码中还有问题!求解?

2 个回复

倒序浏览
运行了一下 没问题啊  挺正常的啊
回复 使用道具 举报
我运行时1首先出现null...null  
后就正常了!我就郁闷了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马