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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© trhthyj 中级黑马   /  2014-4-15 08:30  /  828 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class Res
  2. {
  3.         String name;
  4.         String sex;
  5.         boolean flag=false;
  6. }

  7. class Input implements Runnable
  8. {
  9.         private Res r;
  10.         Input(Res r)
  11.         {
  12.                 this.r=r;
  13.         }
  14.         public  void run ()
  15.         {
  16.                 int x=0;
  17.                 while(true)
  18.                 {
  19.                         synchronized(r)
  20.                         {
  21.                                 if(r.flag)
  22.                                 {
  23.                                         try
  24.                                         {
  25.                                                 r.wait();
  26.                                         }
  27.                                         catch(Exception e)
  28.                                         {
  29.                                         }
  30.                                 }
  31.                                 if(x==0)
  32.                                 {
  33.                                         r.name="wangming";
  34.                                         r.sex="nan";
  35.                                 }
  36.                                 else
  37.                                 {
  38.                                         r.name="王明";
  39.                                         r.sex="男";
  40.                                 }
  41.                                 x=(x+1)%2;
  42.                                 r.flag=true;
  43.                                 r.notify();
  44.                         }
  45.                 }
  46.         }
  47.        
  48. }

  49. class Output implements Runnable
  50. {
  51.         private Res r;
  52.         Output(Res r)
  53.         {
  54.                 this.r=r;
  55.         }
  56.         public void run()
  57.         {
  58.                 while(true)
  59.                 {
  60.                         synchronized(r)
  61.                         {
  62.                                 if(!r.flag)
  63.                                 {
  64.                                         try
  65.                                         {
  66.                                                 r.wait();
  67.                                         }
  68.                                         catch(Exception e)
  69.                                         {
  70.                                         }
  71.                                 }
  72.                                 else
  73.                                 {
  74.                                         System.out.println(r.name+"........"+r.sex);
  75.                                 }
  76.                                 r.flag=false;
  77.                                 r.notify();
  78.                         }
  79.                 }
  80.         }
  81. }

  82. class InputOutputDemo
  83. {
  84.         public static void main(String[] args)
  85.         {
  86.                 Res r=new Res();
  87.                 Input in=new Input(r);
  88.                 Output out=new Output(r);
  89.                 Thread t1=new Thread(in);
  90.                 Thread t2=new Thread(out);
  91.                 t1.start();
  92.                 t2.start();

  93.         }
  94. }





复制代码


为什么出来是这样子:

评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1

查看全部评分

2 个回复

倒序浏览
else
                                {
                                        System.out.println(r.name+"........"+r.sex);
                                }
这里有错,不要加else,如果输出线程在上面等待,等输入线程输入完成后,CPU切换到输出线程,由于输出语句在else里面,就不再输出了,就会又把flag改为false,这样中间就少了一句,这样就极有可能会出现两句同样的语句,而不是交替输出
回复 使用道具 举报
你这是一个“生产者与消费者”问题吧!
  1. package wff.ithem;

  2. class Res
  3. {
  4.         String name;
  5.         String sex;
  6.         boolean flag=false;
  7. }

  8. class Input implements Runnable
  9. {
  10.         private Res r;
  11.         Input(Res r)
  12.         {
  13.                 this.r=r;
  14.         }
  15.         public  void run ()
  16.         {
  17.                 int x=0;
  18.                 while(true)
  19.                 {
  20.                         synchronized(r)
  21.                         {
  22.                                 if(r.flag)                                //当r.flag的值为true时,表时只能打印值
  23.                                 {
  24.                                         try
  25.                                         {
  26.                                                 r.wait();
  27.                                         }
  28.                                         catch(Exception e)
  29.                                         {
  30.                                         }
  31.                                 }
  32.                                 if(x==0)
  33.                                 {
  34.                                         r.name="wangming";
  35.                                         r.sex="nan";
  36.                                 }
  37.                                 else
  38.                                 {
  39.                                         r.name="王明";
  40.                                         r.sex="男";
  41.                                 }
  42.                                 x=(x+1)%2;                        //表示x的值只能取0或着1
  43.                                 r.flag=true;
  44.                                 r.notify();                        //唤醒等待的线程
  45.                         }
  46.                 }
  47.         }
  48.         
  49. }

  50. class Output implements Runnable
  51. {
  52.         private Res r;
  53.         Output(Res r)
  54.         {
  55.                 this.r=r;
  56.         }
  57.         public void run()
  58.         {
  59.                 while(true)
  60.                 {
  61.                         synchronized(r)
  62.                         {
  63.                                 if(!r.flag)
  64.                                 {
  65.                                         try
  66.                                         {
  67.                                                 r.wait();
  68.                                         }
  69.                                         catch(Exception e)
  70.                                         {
  71.                                         }
  72.                                 }
  73.                                 /**
  74.                                  * 错误在这里,这里不应该有else,
  75.                                  * 或着else时把下面三条语句都加进else中
  76.                                  *
  77.                                  */
  78.                                 System.out.println(r.name+"........"+r.sex);
  79.                                 r.flag=false;
  80.                                 r.notify();
  81.                         }
  82.                 }
  83.         }
  84. }

  85. public class InputOutputDemo
  86. {
  87.         public static void main(String[] args)
  88.         {
  89.                 Res r=new Res();
  90.                 Input in=new Input(r);
  91.                 Output out=new Output(r);
  92.                 Thread t1=new Thread(in);
  93.                 Thread t2=new Thread(out);
  94.                 t1.start();
  95.                 t2.start();

  96.         }
  97. }




复制代码

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马