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

© yting_xmei1129 中级黑马   /  2013-9-25 23:32  /  1456 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

下面是代码--->
  1. package yting.day11.thread;

  2. public class ThreadCommunication {

  3.         public static void main(String[] args) {
  4.                 Resource r = new Resource();
  5.                 new Thread(new Input(r)).start();
  6.                 new Thread(new Output(r)).start();
  7.         }

  8. }

  9. class Resource {
  10.         String name;
  11.         String sex;
  12.         boolean flag = false;
  13. }

  14. class Input implements Runnable {
  15.         private Resource r;

  16.         public Input(Resource r) {
  17.                 super();
  18.                 this.r = r;
  19.         }

  20.         @Override
  21.         public void run() {
  22.                 int f = 0;
  23.                 while (true) {
  24.                         synchronized (r) {
  25.                                 if(r.flag){
  26.                                         try {
  27.                                                 wait();
  28.                                         } catch (InterruptedException e) {
  29.                                                 e.printStackTrace();
  30.                                         }
  31.                                 }
  32.                                 if (f == 0) {
  33.                                         r.name = "zhangsan";
  34.                                         r.sex = "男";
  35.                                 } else {
  36.                                         r.name = "lili";
  37.                                         r.sex = "女";
  38.                                 }
  39.                                 f = (f + 1) % 2;
  40.                                
  41.                                 r.flag = true;
  42.                                 notify();
  43.                         }
  44.                 }
  45.         }

  46. }

  47. class Output implements Runnable {
  48.         private Resource r;

  49.         public Output(Resource r) {
  50.                 super();
  51.                 this.r = r;
  52.         }

  53.         @Override
  54.         public void run() {
  55.                 while (true) {
  56.                         synchronized (r) {
  57.                                 if(!r.flag){
  58.                                         try {
  59.                                                 wait();
  60.                                         } catch (InterruptedException e) {
  61.                                                 e.printStackTrace();
  62.                                         }
  63.                                 }
  64.                                 System.out.println(r.name + "..." + r.sex);
  65.                                
  66.                                 r.flag = false;
  67.                                 notify();
  68.                         }
  69.                 }
  70.         }

  71. }
复制代码
下面是运行结果--->
  1. Exception in thread "Thread-0" zhangsan...男
  2. java.lang.IllegalMonitorStateException
  3.         at java.lang.Object.notify(Native Method)
  4.         at yting.day11.thread.Input.run(ThreadCommunication.java:49)
  5.         at java.lang.Thread.run(Thread.java:662)
  6. Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
  7.         at java.lang.Object.notify(Native Method)
  8.         at yting.day11.thread.Output.run(ThreadCommunication.java:78)
  9.         at java.lang.Thread.run(Thread.java:662)
复制代码
一直抱这个异常,怎么回事啊?坐等大神指导、、、谢了先!

2 个回复

倒序浏览
synchronized (r)
既然你同步的是这个对象,你就应该调用这个对象的wait() notify()方法 去告知线程等待和唤醒
  1. package com.test;

  2. public class Demo1 {

  3.         public static void main(String[] args) {
  4.                 Resource r = new Resource();
  5.                 new Thread(new Input(r)).start();
  6.                 new Thread(new Output(r)).start();
  7.         }

  8. }

  9. class Resource extends Thread{
  10.         String name;
  11.         String sex;
  12.         boolean flag = false ;
  13.        
  14. }

  15. class Input extends Thread {
  16.         private Resource r;

  17.         public Input(Resource r) {
  18.                 super();
  19.                 this.r = r;
  20.         }

  21.         @Override
  22.         public void run() {
  23.                 int f = 0;
  24.                 while (true) {
  25.                         synchronized (r) {
  26.                                 if (r.flag) {
  27.                                         try {
  28.                                                 r.wait();
  29.                                         } catch (InterruptedException e) {
  30.                                                 e.printStackTrace();
  31.                                         }
  32.                                 }
  33.                                 if (f == 0) {
  34.                                         r.name = "zhangsan";
  35.                                         r.sex = "男";
  36.                                 } else {
  37.                                         r.name = "lili";
  38.                                         r.sex = "女";
  39.                                 }
  40.                                 f = (f + 1) % 2;

  41.                                 r.flag = true;
  42.                                 r.notify();
  43.                         }
  44.                 }
  45.         }

  46. }

  47. class Output extends Thread {
  48.         private Resource r;

  49.         public Output(Resource r) {
  50.                 super();
  51.                 this.r = r;
  52.         }

  53.         @Override
  54.         public void run() {
  55.                 while (true) {
  56.                         synchronized (r) {
  57.                                 if (!r.flag) {
  58.                                         try {
  59.                                                 r.wait();
  60.                                         } catch (InterruptedException e) {
  61.                                                 e.printStackTrace();
  62.                                         }
  63.                                 }
  64.                                 System.out.println(r.name + "..." + r.sex);

  65.                                 r.flag = false;
  66.                                 r.notify();
  67.                         }
  68.                 }
  69.         }

  70. }
复制代码
你试试
回复 使用道具 举报
肖亚光 发表于 2013-9-27 15:33
synchronized (r)
既然你同步的是这个对象,你就应该调用这个对象的wait() notify()方法 去告知线程等待和 ...

谢谢了,后来饿才发现弄错了呢!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马