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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 学习代码 中级黑马   /  2014-3-16 10:12  /  715 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class Demo1
  2. {
  3.         String name;
  4.         String sex;
  5.         boolean flag=false;
  6. }
  7. class Input implements Runnable
  8. {
  9.         private Demo1 e;
  10.         Input(Demo1 e)
  11.         {
  12.                 this.e=e;
  13.         }
  14.         public void run()
  15.         {        int a=0;
  16.                 while(true)
  17.                 {        synchronized(e)
  18.                         {       
  19.                                 if(e.flag)
  20.                                 {        try{e.wait();}catch(Exception e){}
  21.                                         if(a==0)
  22.                                         {
  23.                                                 e.name="zhangsan";
  24.                                                 e.sex="man";
  25.                                         }
  26.                                         else
  27.                                         {
  28.                                                 e.name="丽丽";
  29.                                                 e.sex="女女女女女";
  30.                                         }
  31.                                         a = (a+1)%2;
  32.                                         e.flag=true;
  33.                                         e.notify();
  34.                         }
  35.                 }
  36.                
  37.         }
  38. }

  39. }
  40. class Output implements Runnable
  41. {
  42.         private Demo1 e;
  43.         Output(Demo1 e)
  44.         {
  45.                 this.e=e;
  46.         }
  47.         public void run()
  48.         {
  49.                 while(true)
  50.                 {        synchronized(e)
  51.                         {        if(!e.+flag)
  52.                                 {        try{e.wait();}catch(Exception e){}
  53.                                         System.out.println(e.name+";;;;"+e.sex);
  54.                                         e.flag=false;
  55.                                         e.notify();
  56.                                 }
  57.                         }
  58.                 }
  59.         }
  60. }
  61. class Demo
  62. {
  63.         public static void main(String[] args)
  64.         {
  65.                 Demo1 e = new Demo1();
  66.                 Input in = new Input(e);
  67.                 Output out = new Output(e);
  68.                 Thread t = new Thread(in);
  69.                 Thread t1 = new Thread(out);
  70.                 t.start();
  71.                 t1.start();
  72.         }
  73. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

6 个回复

倒序浏览
:L什么问题啊 请指明
回复 使用道具 举报
梦里花-静 发表于 2014-3-16 10:27
什么问题啊 请指明

头都被弄乱了  问题都没写 实在抱歉   我就是想问一下 为什么  不出结果呢  
回复 使用道具 举报
  1. class Demo1
  2. {
  3.         String name;
  4.         String sex;
  5.         boolean flag=false;
  6. }
  7. class Input implements Runnable
  8. {
  9.         private Demo1 e;
  10.         Input(Demo1 e)
  11.         {
  12.                 this.e=e;
  13.         }
  14.         public void run()
  15.         {        int a=0;
  16.                 while(true)
  17.                 {        synchronized(e)
  18.                         {        
  19.                                 if(e.flag)
  20.                                         try{e.wait();}catch(Exception e){}
  21.                                         if(a==0)
  22.                                         {
  23.                                                 e.name="zhangsan";
  24.                                                 e.sex="man";
  25.                                         }
  26.                                         else
  27.                                         {
  28.                                                 e.name="丽丽";
  29.                                                 e.sex="女女女女女";
  30.                                         }
  31.                                         a = (a+1)%2;
  32.                                         e.flag=true;
  33.                                         e.notify();
  34.                         
  35.                 }
  36.                
  37.         }
  38. }

  39. }
  40. class Output implements Runnable
  41. {
  42.         private Demo1 e;
  43.         Output(Demo1 e)
  44.         {
  45.                 this.e=e;
  46.         }
  47.         public void run()
  48.         {
  49.                 while(true)
  50.                 {        synchronized(e)
  51.                         {        if(!e.flag)
  52.                                         try{e.wait();}catch(Exception e){}
  53.                                         System.out.println(e.name+";;;;"+e.sex);
  54.                                         e.flag=false;
  55.                                         e.notify();
  56.                                 
  57.                         }
  58.                 }
  59.         }
  60. }
  61. class Demo
  62. {
  63.         public static void main(String[] args)
  64.         {
  65.                 Demo1 e = new Demo1();
  66.                 Input in = new Input(e);
  67.                 Output out = new Output(e);
  68.                 Thread t = new Thread(in);
  69.                 Thread t1 = new Thread(out);
  70.                 t.start();
  71.                 t1.start();
  72.         }
  73. }
复制代码

仔细看一下代码。

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

回复 使用道具 举报

好的 谢谢  问题找到了  
回复 使用道具 举报
52行   多了个加号   20  34  53  57  把那个大括号去掉   

你编程出现的问题是    if(e.flag)  if(!e.flag)  判断的范围不确定     这两个if主要是判断范围  只有 wait()方法  你的范围太大啦。。   你再看看毕老师的视频

评分

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

查看全部评分

回复 使用道具 举报
一年_Hei 来自手机 中级黑马 2014-3-16 12:11:34
7#
多了一个输出里判断标记多了+
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马