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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© java小兵 中级黑马   /  2014-12-12 11:46  /  882 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 java小兵 于 2015-1-26 21:35 编辑
  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.                                         try{r.wait();}catch(Exception e){}
  23.                                 
  24.                                 if (x==0)
  25.                                 {
  26.                                 
  27.                                         r.name = "mike";
  28.                                         r.sex = "man";
  29.                                        
  30.                                 }
  31.                                 else
  32.                                 {
  33.                                         r.name = "Lucy";
  34.                                         r.sex = "woman";
  35.                                 }
  36.                                 x = (x+1)%2;
  37.                                 r.flag = true;
  38.                                 r.notify();
  39.                         }
  40.                 }
  41.         }
  42. }
  43. class Outer implements Runnable
  44. {
  45.         private Res r;
  46.         Outer(Res r)
  47.         {
  48.                 this.r = r;
  49.         }
  50.         public void run()
  51.         {
  52.                 while (true)
  53.                 {
  54.                         synchronized(r)
  55.                         {
  56.                                 if(!r.flag)
  57.                                         try{wait();}catch(Exception e){}
  58.                                 System.out.println(r.name+"--"+r.sex);
  59.                                 r.flag = false;
  60.                                 r.notify();
  61.                         }               
  62.                 }
  63.         }
  64. }
  65. class  Demo2_3
  66. {
  67.         public static void main(String[] args)
  68.         {
  69.                 Res r = new Res();
  70.                 Input in = new Input(r);
  71.                 Outer out = new Outer(r);

  72.                 Thread t1 = new Thread(in);
  73.                 Thread t2 =  new Thread(out);

  74.                 t1.start();
  75.                 t2.start();
  76.                 System.out.println("Hello World!");
  77.         }
  78. }
复制代码


代码是照抄的。结果跟没加wait notify是一样的怎么回事呢?

1 个回复

倒序浏览
问题解决了。Outer少些一个r.wait()
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马