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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张智文 中级黑马   /  2013-8-10 08:32  /  1212 人查看  /  7 人回复  /   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 s;
  10.         Input(Res s)
  11.         {
  12.                 this.s = s;
  13.         }
  14.         public void run()
  15.         {
  16.                 //boolean b = true;
  17.                 int x = 0;
  18.                 while(true)
  19.                 {
  20.                         synchronized(Res.class)
  21.                         {
  22.                                 if(s.flag)
  23.                                         try{Res.class.wait();}catch(Exception e){}
  24.                                 if(x==0)
  25.                                 {
  26.                                         s.name = "Jack";
  27.                                         s.sex = "man";
  28.                                         //b=false;
  29.                                 }
  30.                                 else
  31.                                 {
  32.                                         s.name = "丽丽";
  33.                                         s.sex = "女";
  34.                                         //b=true;
  35.                                 }
  36.                                 x = (x+1)%2;
  37.                                 s.flag = true;
  38.                                 Res.class.notify();
  39.                         }
  40.                 }
  41.         }
  42. }

  43. class Output implements Runnable
  44. {
  45.         private Res s;
  46.         Output(Res s)
  47.         {
  48.                 this.s = s;
  49.         }
  50.         public void run()
  51.         {
  52.                
  53.                 while(true)
  54.                 {
  55.                         
  56.                         synchronized(Res.class)
  57.                         {
  58.                                 if(!s.flag)
  59.                                 try{Res.class.wait();}catch(Exception e){}
  60.                                 System.out.println(s.name+"...."+s.sex);
  61.                                 s.flag = true;
  62.                                 Res.class.notify();
  63.                         }
  64.                         
  65.                 }
  66.         }
  67. }

  68. public class InputOutputDemo
  69. {
  70.         public static void main(String[] args)
  71.         {
  72.                 Res s = new Res();
  73.                 Input i = new Input(s);
  74.                 Output o = new Output(s);
  75.                
  76.                 Thread t1 = new Thread(i);
  77.                 Thread t2 = new Thread(o);
  78.                
  79.                 t1.start();
  80.                 t2.start();
  81.         }
  82. }

复制代码
//照着毕老师的一点点自己打的。结果又不对。愁啊。。求帮忙看看

评分

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

查看全部评分

7 个回复

倒序浏览
亲,我想说我运行的没有错误。。。


我想之所你你运行会出错是因为你的文件名跟类名不一致导致的,你把文件名改成InputOutputDemo.java看下。
回复 使用道具 举报
杨璐敏 发表于 2013-8-10 09:18
亲,我想说我运行的没有错误。。。

是结果和老师的不一样。。
回复 使用道具 举报
多线程的结果每次都不一样的。。。
回复 使用道具 举报
杨璐敏 发表于 2013-8-10 09:18
亲,我想说我运行的没有错误。。。

亲,不错哦
回复 使用道具 举报

虽然进步比较慢,,,但总比停滞不前好多了是吧

嘿嘿

点评

膜拜  发表于 2013-8-10 20:43
回复 使用道具 举报
我来看看......
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马