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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 不破大地 中级黑马   /  2013-6-18 20:50  /  1640 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

问题(1):在下面的代码中,为何运行时出错,
public class Threadcomunicate
{
     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();
     }
}
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(r)
   {
    if(r.flag)
     try {r.wait();}catch(Exception e){}
       if(x==0)
       {
             r.name="mike";
             r.sex="man";
       }
       else
       {
             r.name="lili";
             r.sex="women";
       }
   }
   x=(x+1)%2;
   r.flag=true;
   r.notify();
  }
}
}
class Output implements Runnable
{
res r;
Object obj=new Object();
Output(res r)
{
  this.r=r;
}
public void run()
{
  while(true)
  {
   synchronized(r)
   {
    if(!r.flag)
     try {r.wait();}catch(Exception e){}
        System.out.println(r.name+".."+r.sex);
        r.flag=false;
        r.notify();
   }
  }
}
}
class res
{
String name;
String sex;
boolean flag=false;
}public class 代码体现
{
     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();
     }
}
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(r)
   {
    if(r.flag)
     try {r.wait();}catch(Exception e){}
       if(x==0)
       {
             r.name="mike";
             r.sex="man";
       }
       else
       {
             r.name="lili";
             r.sex="women";
       }
   }
   x=(x+1)%2;
   r.flag=true;
   r.notify();
  }
}
}
class Output implements Runnable
{
res r;
Object obj=new Object();
Output(res r)
{
  this.r=r;
}
public void run()
{
  while(true)
  {
   synchronized(r)
   {
    if(!r.flag)
     try {r.wait();}catch(Exception e){}
        System.out.println(r.name+".."+r.sex);
        r.flag=false;
        r.notify();
   }
  }
}
}
class res
{
String name;
String sex;
boolean flag=false;
}

问题(2):flag初始化值不是false假吗,那为何毕姥爷解释  这句“if(flag), try {r.wait();}catch(Exception e){}”代码的时候,说如果flag为真,则该线程等待,不是应该为如果flag为假时则线程等待吗?
还望各位高人点拨哈。。。

评分

参与人数 1技术分 +1 收起 理由
孙百鑫 + 1

查看全部评分

6 个回复

倒序浏览
代码好乱,好些重复的...
//问题(1):在下面的代码中,为何运行时出错,
public class Threadcomunicate
{
     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();
     }
}
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(r)
                  {
                          if(r.flag)
                                  try {r.wait();}catch(Exception e){}
                          if(x==0)
                          {       
                                  r.name="mike";
                                  r.sex="man";
                          }
                          else
                          {
                                  r.name="lili";
                                  r.sex="women";
                          }
                  r.flag=true;
                  r.notify();
                  }
                  x=(x+1)%2;
                 
                  }
        }
}
class Output implements Runnable
{
        res r;
        Object obj=new Object();
        Output(res r)
        {
                this.r=r;
        }
        public void run()
        {
                while(true)
                {
                        synchronized(r)
                        {
                                if(!r.flag)
                                        try {r.wait();}catch(Exception e){}
                                System.out.println(r.name+".."+r.sex);
                                r.flag=false;
                                r.notify();
                        }
                }
        }
}

class res
{
        String name;
        String sex;
        boolean flag=false;
}

评分

参与人数 1技术分 +1 收起 理由
孙百鑫 + 1

查看全部评分

回复 使用道具 举报
第一个问题
  1. public void run()
  2. {
  3.   int x=0;
  4.   while(true)
  5.   {
  6.    synchronized(r)
  7.    {
  8.     if(r.flag)
  9.      try{r.wait();}catch (Exception e) { }
  10.        if(x==0)
  11.        {
  12.              r.name="mike";
  13.              r.sex="man";
  14.        }
  15.        else
  16.        {
  17.              r.name="lili";
  18.              r.sex="women";
  19.        }
  20.   // } //同步块的主体定义错误,下面对flag和notify()的操作都是两个线程的共有数据,应该写在同步内。
  21.    x=(x+1)%2;
  22.    r.flag=true;
  23.    r.notify();
  24.    }//同步主体定义到这里就好了。
  25.   }
  26. }
复制代码
第二个问题,if(flag)  try {r.wait();}catch(Exception e){} --- flag为true是这个if语句运行主体的条件,只有if主体能运行,才会运行到r.wait()这个线程等待的方法。老师说的没错,如果按楼主说flag为false,则if语句中的主体部分不能运行到,也就没法执行r.wait()方法了。

评分

参与人数 1技术分 +1 收起 理由
孙百鑫 + 1

查看全部评分

回复 使用道具 举报
出错位置.
r.flag=true;
r.notify();
应该放入同步锁中

if(flag)中flag判断为真,执行if条件语句
if(!flag)中flag判断为假才执行if条件语句
回复 使用道具 举报
楼主您好,由于帖子长时间没有动态,我已经讲帖子改成已解决了,如有问题,可以私聊我。{:soso_e100:}
回复 使用道具 举报
楼主现在解决了没,我和你遇到同样的问题,这两天一直在纠结这个问题,如果你知道了麻烦告诉我哈,痛苦死了。。。
回复 使用道具 举报
我刚刚弄明白你的第二个问题,我觉得这点你不要局限于老师的代码。按照老师的思路,我稍微改动了他的代码,你看看是不是比较容易理解一点
if(r.flag == true)        //满足条件表示属性有值,需要输出,输入线程等待
if(r.flag == false)        //满足条件表示属性没值,需要赋值,输出线程等待
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马