黑马程序员技术交流社区

标题: 求指导!刚刚练习了多线程代码,编译能通过,运行异常。 [打印本页]

作者: 嘉Ming    时间: 2015-5-27 13:12
标题: 求指导!刚刚练习了多线程代码,编译能通过,运行异常。
  1. class ZiYuan
  2. {
  3.         private String name;
  4.         private String sex;
  5.         private boolean flag;

  6.         public void set(String name,String sex)
  7.         {
  8.                 if(flag)
  9.                         try{this.wait();}catch(Exception e){ }
  10.                 this.name=name;
  11.                 this.sex=sex;
  12.                 this.flag=true;
  13.                 this.notify();
  14.         }

  15.         public void ShuChu()
  16.         {
  17.                 if(!flag)
  18.                         try{this.wait();}catch(Exception e){ }
  19.                 System.out.println(name+"====="+sex);
  20.                 this.flag=false;
  21.                 this.notify();
  22.         }
  23. }

  24. class Input implements Runnable
  25. {
  26.         private ZiYuan r;
  27.         Input(ZiYuan r)  
  28.         {
  29.                 this.r=r;
  30.         }

  31.         public void run()
  32.         {
  33.                 int x=0;
  34.                 while(true)
  35.                 {
  36.                         if(x==0)
  37.                                 r.set("iron man","male");
  38.                         else
  39.                                 r.set("朴信惠","女");
  40.                 x=(x+1)%2;
  41.                 }
  42.         }
  43. }

  44. class Output implements Runnable
  45. {
  46.         private ZiYuan r;
  47.         Output(ZiYuan r)
  48.         {
  49.                 this.r=r;
  50.         }

  51.         public void run()
  52.         {
  53.                 while(true)
  54.                 {
  55.                        
  56.                         r.ShuChu();
  57.                 }
  58.         }
  59. }

  60. class TongXun02
  61. {
  62.         public static void main(String[] args)
  63.         {
  64.                 ZiYuan r =new ZiYuan();
  65.                 new Thread(new Input(r)).start();
  66.                 new Thread(new Output(r)).start();
  67.                
  68.         }
  69. }
复制代码


出现的提示是:Exception in thread "Thread-0" iron man=====male
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
        at java.lang.Object.notify(Native Method)
        at ZiYuan.set(TongXun02.java:14)
        at Input.run(TongXun02.java:41)
        at java.lang.Thread.run(Thread.java:745)
java.lang.IllegalMonitorStateException
        at java.lang.Object.notify(Native Method)
        at ZiYuan.ShuChu(TongXun02.java:23)
        at Output.run(TongXun02.java:62)
        at java.lang.Thread.run(Thread.java:745)

作者: forTomorrow    时间: 2015-5-27 15:30
没加同步锁synchronized就用this,flag标志起初应该置空

package learn_say;

class ZiYuan {
        private String name;
        private String sex;
        private boolean flag = false;//标签
        //设置资源属性(加同步锁确保线程 安全)
        public synchronized void set(String name, String sex) {
                if (flag)
                        try {
                                this.wait();
                        } catch (Exception e) {
                        }
                this.name = name;
                this.sex = sex;
                this.flag = true;
                this.notify();
        }
        //打印资源属性
        public synchronized void ShuChu() {
                if (!flag)
                        try {
                                this.wait();
                        } catch (Exception e) {
                        }
                System.out.println(name + "=====" + sex);
                this.flag = false;
                this.notify();
        }
}

class Input implements Runnable {
        private ZiYuan r;

        Input(ZiYuan r) {
                this.r = r;
        }

        public void run() {
                int x = 0;
                //循环交替改变资源属性值
                while (true) {
                        if (x == 0)
                                r.set("iron man", "male");
                        else
                                r.set("朴信惠", "女");
                        x = (x + 1) % 2;
                }

        }
}

class Output implements Runnable {
        private ZiYuan r;

        Output(ZiYuan r) {
                this.r = r;
        }

        public void run() {
                while (true) {
                        r.ShuChu();
                }
        }
}

class Test3 {
        public static void main(String[] args) {
                ZiYuan r = new ZiYuan();
                new Thread(new Input(r)).start();
                new Thread(new Output(r)).start();

        }
}
作者: 嘉Ming    时间: 2015-5-27 16:02
forTomorrow 发表于 2015-5-27 15:30
没加同步锁synchronized就用this,flag标志起初应该置空

package learn_say;

原来是我忘记在函数上加synchronized了,谢谢帮助
作者: forTomorrow    时间: 2015-5-27 16:40
嘉Ming 发表于 2015-5-27 16:02
原来是我忘记在函数上加synchronized了,谢谢帮助

你是怎么搞到那么多技术分的,我刚注册两三天,现在在过第三关啊,技术分不够10分,郁闷死啊
作者: 小车车    时间: 2015-5-27 20:59
我只知flag没有初始化,还是没有看到还要在函数前加synchronized。
作者: 嘉Ming    时间: 2015-5-28 00:29
forTomorrow 发表于 2015-5-27 16:40
你是怎么搞到那么多技术分的,我刚注册两三天,现在在过第三关啊,技术分不够10分,郁闷死啊 ...

我每天都登陆,然后有空就去评论,这样会拿到黑马币,然后用黑马币换取的。我都这样做两个星期了。我也在准备第三关,边准备边赚币。加油!!你的进展挺快的
作者: 嘉Ming    时间: 2015-5-28 00:36
forTomorrow 发表于 2015-5-27 16:40
你是怎么搞到那么多技术分的,我刚注册两三天,现在在过第三关啊,技术分不够10分,郁闷死啊 ...

其实主要原因是我看了论坛上相关的建议,在前两个阶段就已经注册了,然后每天都花时间逛论坛,所以才有现在的效果。另外要有25个技术分才能分配入学名额,你安排好时间吧,加油。
作者: feng0606    时间: 2015-5-28 00:48
刚刚看完这里,感觉还不是很懂,还得看好久啊.
作者: forTomorrow    时间: 2015-5-28 10:23
嘉Ming 发表于 2015-5-28 00:36
其实主要原因是我看了论坛上相关的建议,在前两个阶段就已经注册了,然后每天都花时间逛论坛,所以才有现 ...

额,那一共要25还是35技术分呢,我之前不知道报名这么复杂,自学的时候没注册过,悲剧啊!!!!
作者: 嘉Ming    时间: 2015-5-29 15:27
forTomorrow 发表于 2015-5-28 10:23
额,那一共要25还是35技术分呢,我之前不知道报名这么复杂,自学的时候没注册过,悲剧啊!!!! ...

一共25分,你可以上这里看看http://bbs.itheima.com/thread-37565-1-1.html
作者: 微凉的暮色    时间: 2015-5-29 15:30
forTomorrow 发表于 2015-5-28 10:23
额,那一共要25还是35技术分呢,我之前不知道报名这么复杂,自学的时候没注册过,悲剧啊!!!! ...

:L 比我速度快多了,看我可怜的技术分




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