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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© troy健 中级黑马   /  2014-7-18 10:28  /  1231 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package cn.itheima.day11;

  2. class Res{
  3.         String name;
  4.         String sex;
  5.         boolean flag =false;
  6. }

  7. class Input implements Runnable{
  8.         private Res r;
  9.        
  10.         Input(Res r){
  11.                 this.r = r;
  12.         }
  13.         public void run(){
  14.                 int x = 0;
  15.                 while(true){
  16.                         synchronized(r){
  17.                                 if(r.flag)
  18.                                         try {
  19.                                                 wait();
  20.                                         } catch (InterruptedException e) {
  21.                                                 // TODO Auto-generated catch block
  22.                                                 e.printStackTrace();
  23.                                         }
  24.                                 if(x == 0){
  25.                                         r.name = "mike";
  26.                                         r.sex = "man";
  27.                                 }else{
  28.                                         r.name = "丽丽";
  29.                                         r.sex = "女";
  30.                                 }
  31.                                 x = (x+1)%2;
  32.                                 r.flag = true;
  33.                                 notify();
  34.                                
  35.                         }
  36.                 }
  37.         }
  38. }

  39. class Output implements Runnable{
  40.         private Res r;
  41.        
  42.         Output(Res r){
  43.                 this.r = r;
  44.         }
  45.         public void run(){
  46.                 while(true){
  47.                         synchronized (r) {
  48.                                 if(!r.flag)
  49.                                         try {
  50.                                                 wait();
  51.                                         } catch (InterruptedException e) {
  52.                                                 // TODO Auto-generated catch block
  53.                                                 e.printStackTrace();
  54.                                         }
  55.                                 System.out.println(r.name + "...." + r.sex);
  56.                                 r.flag = false;
  57.                                 notify();
  58.                                
  59.                         }
  60.                 }
  61.         }
  62. }


  63. public class 线程间通信 {

  64.        
  65.         public static void main(String[] args) {
  66.                 // TODO Auto-generated method stub
  67.                 Res r = new Res();
  68.                
  69.                 Input in = new Input(r);
  70.                 Output out = new Output(r);
  71.                
  72.                 Thread t1 =new Thread(in);
  73.                 Thread t2 = new Thread(out);
  74.                 t1.start();
  75.                 t2.start();
  76.         }

  77. }
复制代码


这是代码,毕老师day12的示例代码。问题:
  1. mike....man
  2. Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
  3.         at java.lang.Object.notify(Native Method)
  4.         at cn.itheima.day11.Input.run(线程间通信.java:35)
  5.         at java.lang.Thread.run(Thread.java:745)
  6. Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
  7.         at java.lang.Object.notify(Native Method)
  8.         at cn.itheima.day11.Output.run(线程间通信.java:60)
  9.         at java.lang.Thread.run(Thread.java:745)
复制代码


求解释

3 个回复

倒序浏览
你两个线程中的wait()方法与notify()方法均未加上调用的对象,就默认是线程自身的wait()方法与notify(),即Input与Output类自身的wait()方法与notify()。这样两个的线程的锁是不一样的。我们要实现同步,锁要求一样,将线程中的这四句代码分别改为: r.wait()与r.notify()。
回复 使用道具 举报
notify方法,唤醒wait了的方法,那么多的线程,你要唤醒哪一个,唤醒拥有相同的锁在线程池中等待的第一个线程因此,上面的程序应该在wait时应该写成r.wait(); notify写为r.notify();,线程同步的前提,锁相同,操作的数据相同
修改后程序结果如此:

8BH(P(8OI`I{][2$W@85CVR.jpg (47.2 KB, 下载次数: 20)

8BH(P(8OI`I{][2$W@85CVR.jpg
回复 使用道具 举报
方法的调用需要对象,控制台报错的那几行代码,直接就一个裸体的方法,没有用对象去调用
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马