黑马程序员技术交流社区

标题: 麻烦高手指点这问题究竟出到哪里了, [打印本页]

作者: fenzheng    时间: 2014-6-2 23:04
标题: 麻烦高手指点这问题究竟出到哪里了,
  1. package jichu;

  2. class Res{
  3.         String name ;
  4.         String sex ;
  5.         boolean flag=false;
  6. }
  7. class Input implements Runnable{
  8.         private Res r;
  9.         Input(Res r){
  10.                 this.r=r;
  11.         }
  12.         public void run(){
  13.                 int x=0;
  14.                 while (true)
  15.                 {
  16.                         synchronized (r) {
  17.                                 if (r.flag) {try {r.wait();} catch (Exception e) {}
  18.                                 if (x==0) {r.name="mike";r.sex="man";}       
  19.                                 else {
  20.                                         r.name="lili";
  21.                                         r.sex="women";
  22.                                 }
  23.                                 x=(x+1)%2;
  24.                                 r.flag=true;
  25.                                 r.notify();
  26.                                 }
  27.                         }
  28.                 }
  29.         }
  30.        
  31. }
  32. class Output implements Runnable{
  33.         private Res r;
  34.         Output(Res r)
  35.         {
  36.                 this.r=r;
  37.         }
  38.         public void run(){
  39.                 while (true){
  40.                         synchronized (r) {if(!r.flag)//判断标记
  41.                                 try {r.wait();
  42.                                
  43.                         } catch (Exception e) {
  44.                                 }
  45.                                 System.out.println(r.name+"......."+r.sex);
  46.                                 r.flag=false;
  47.                                 r.notify();
  48.                         }
  49.                         }
  50.                 }
  51.         }
  52. public class InPutOutPutDemo {
  53.         public static void main (String [] args){
  54.                 Res r= new Res ();
  55.                 System.out.println("开始了");
  56.                 Input in=new Input (r);
  57.                 System.out.println("输入线程建立");
  58.                 Output out=new Output(r);
  59.                 System.out.println("输出线程建立");
  60.                 Thread t1=new Thread(in);
  61.                 Thread t2=new Thread (out);
  62.                 t1.start();
  63.                 System.out.println("开启线程1");
  64.                 t2.start();
  65.                 System.out.println("开启线程2");
  66.         }

  67. }
复制代码
运行结果如图片所示,明明程序都走了一遍为什么不出我想要的结果?什么原因啊?错哪里了?

搜狗截图20140602230024.png (32.55 KB, 下载次数: 18)

搜狗截图20140602230024.png

作者: 27ZJQ    时间: 2014-6-2 23:12
还没学到,不懂。
作者: 月光海    时间: 2014-6-2 23:18
把Input类中的run方法的代码改成这样
  1. public void run() {
  2.                 int x = 0;
  3.                 while (true) {
  4.                         synchronized (r) {
  5.                                 if (r.flag) {
  6.                                         try {
  7.                                                 r.wait();
  8.                                         } catch (Exception e) {
  9.                                         }}
  10.                                         if (x == 0) {
  11.                                                 r.name = "mike";
  12.                                                 r.sex = "man";
  13.                                         } else {
  14.                                                 r.name = "lili";
  15.                                                 r.sex = "women";
  16.                                         }
  17.                                         x = (x + 1) % 2;
  18.                                         r.flag = true;
  19.                                         r.notify();
  20.                                
  21.                         }
  22.                 }
  23.         }
复制代码
判断标记的if语句是独立的,和对资源赋值的语句没有关系,
作者: fenzheng    时间: 2014-6-2 23:35
呵呵,是的,修改了好了,太谢谢了!!
作者: fenzheng    时间: 2014-6-2 23:37
月光海 发表于 2014-6-2 23:18
把Input类中的run方法的代码改成这样判断标记的if语句是独立的,和对资源赋值的语句没有关系, ...

是的,刚才按你说的,修改好了,太谢谢你了!
作者: 北极£莜蓝    时间: 2014-6-2 23:50
还没学到.............膜拜
作者: superob123    时间: 2014-6-3 00:40
月光海 发表于 2014-6-2 23:18
把Input类中的run方法的代码改成这样判断标记的if语句是独立的,和对资源赋值的语句没有关系, ...

学到了,原来这样




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2