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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 小太阳大开心 于 2014-6-30 11:29 编辑

class Rec
{
        private String name;
        private String sex;
        private boolean flag;
        public   synchronized  void  set(String name,String sex)
        {
                if (flag)
                {
                        try
                        {
                                this.wait();
                        }
                        catch (Exception e)
                        {
                        }
                }
                this.name=name;
                this.sex=sex;
                flag=true;
                this.notify();
        }
        public  synchronized void out()
        {
                if (flag)
                {
                        try
                        {
                                this.wait();
                        }
                        catch (Exception e)
                        {
                        }
                }
                System.out.println(name+"......"+sex);
                flag=false;
                this.notify();
        }
}

class Input implements Runnable
{
        private Rec r;
        Input(Rec r)
        {
                this.r=r;
        }
        public void run()
        {
                int x=0;
                while (true)
                {
                        if (x==0)
                        {
                                r.set("baixue","man");
                        }
                        else
                                r.set("cxy","girl");
                        x=(x+1)%2;
                }
               
        }
}
class Output implements Runnable
{
        private Rec r;
        Output(Rec r)
        {
                this.r=r;
        }
        public void run()
        {
                while (true )
                {
                        r.out();
                }
        }
}
class InputOutputDemo2
{
        public static void main(String[] args)
        {
                Rec r=new Rec();
                new Thread (new Input(r)).start();
                new Thread( new Output(r)).start();
        }
}

4 个回复

倒序浏览
  1. public   synchronized  void  set(String name,String sex)
  2.         {
  3.                 if (flag)
  4.                 {
  5.                         try
  6.                         {
  7.                                 this.wait();
  8.                         }
  9.                         catch (Exception e)
  10.                         {
  11.                         }
  12.                 }
  13.                 this.name=name;
  14.                 this.sex=sex;
  15.                 flag=true;
  16.                 this.notify();
  17.         }
  18.         public  synchronized void out()
  19.         {
  20.                 if (!flag)//这里要非一下。
  21.                 {
  22.                         try
  23.                         {
  24.                                 this.wait();
  25.                         }
  26.                         catch (Exception e)
  27.                         {
  28.                         }
  29.                 }
  30.                 System.out.println(name+"......"+sex);
  31.                 flag=false;
  32.                 this.notify();
  33.         }
复制代码
回复 使用道具 举报

哦哦哦,是的,辛苦啦:handshake
回复 使用道具 举报
楼主不知道看懂了吗。我对问题代码进行了注释。你可以看下。 步骤 。1~~5步

QQ截图20140511111212.png (117.23 KB, 下载次数: 59)

QQ截图20140511111212.png
回复 使用道具 举报
sheng6699 发表于 2014-5-11 11:14
楼主不知道看懂了吗。我对问题代码进行了注释。你可以看下。 步骤 。1~~5步
...

恩恩,好的呢,谢谢,辛苦了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马