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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

KKK230794

初级黑马

  • 黑马币:

  • 帖子:

  • 精华:

© KKK230794 初级黑马   /  2016-1-16 19:52  /  2343 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

30黑马币
  1. 我做了一个线程等待和唤醒机制的练习.
  2. class Resource
  3. {
  4.         private String name;
  5.     private String sex;
  6.         private boolean flag=false;
  7.         public synchronized void intputMethod(String name,String sex)
  8.         {
  9.                 if (flag)
  10.                             try{wait();}catch (InterruptedException e){}  
  11.            else
  12.                          {      
  13.                                 this.name=name;
  14.                                 this.sex=sex;
  15.                                 flag=true;
  16.                                 notify();
  17.          }
  18.                                  
  19.         }
  20.         public synchronized void outputMethod()
  21.         {
  22.                                 if(!flag)
  23.                                         try{wait();}catch (InterruptedException e){}
  24.                                 else
  25.                           {     
  26.                                         System.out.println(name+"......."+sex);
  27.                                    flag=false;
  28.                                    notify();
  29.           }
  30.         }
  31. }
  32. class Intput implements Runnable
  33. {
  34.         Resource a;
  35.         Intput(Resource a)
  36.         {
  37.                 this.a=a;
  38.         }
  39.         public void run()
  40.         {
  41.            int x=0;
  42.                 while(true)
  43.                 {
  44.              if(x==0)
  45.                         a.intputMethod("丽丽","女女");
  46.                  else
  47.                         a.intputMethod("MIKE","男男");
  48.                  x=++x%2;
  49.                 }
  50.         }
  51. }
  52. class Output implements Runnable
  53. {
  54.         Resource a;
  55.         Output(Resource a)
  56.         {
  57.                 this.a=a;
  58.         }
  59.         public void run()
  60.         {
  61.                 while(true)
  62.                 {
  63.                 a.outputMethod();
  64.                 }
  65.         }
  66. }
  67. class Demo3
  68. {
  69.         public static void main(String[] args)
  70.         {
  71.                 Resource a=new Resource();
  72.                 Intput t1=new Intput(a);
  73.                 Output t2=new Output(a);
  74.                 Thread p1=new Thread(t1);
  75.                 Thread p2=new Thread(t2);
  76.                 p1.start();
  77.                 p2.start();
  78.         }
  79. }
  80. 理想的输出结果是:丽丽........女女

  81.                                MIKE........男男
  82.                                 丽丽........女女

  83.                                MIKE........男男
  84.               以此无限循环
  85. 实际的输出结果是:丽丽........女女
  86.                              丽丽........女女
  87.                              丽丽........女女
  88.                             MIKE........男男
  89.                             MIKE........男男
  90. 我也知道了是因为多了intputMethod和outputMethod方法中的else所导致,但我不知道为什么求各位大神分析分析,谢谢了!
复制代码



最佳答案

查看完整内容

因为唤醒回来后 就不做if判断了,所以 不能写else,否则视为语法错误,望采纳

2 个回复

倒序浏览
因为唤醒回来后 就不做if判断了,所以 不能写else,否则视为语法错误,望采纳
回复 使用道具 举报
sun288225 发表于 2016-1-16 19:55
因为唤醒回来后 就不做if判断了,所以 不能写else,否则视为语法错误,望采纳 ...

OK我问了下了老师 谢谢了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马