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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ying 中级黑马   /  2013-1-7 13:47  /  1088 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 ying 于 2013-1-8 13:14 编辑

  1. <P>class Res
  2. {
  3. String name;</P>
  4. <P> String sex;</P>
  5. <P> boolean flag = false;</P>
  6. <P>}
  7. /*
  8. Input线程负责
  9. 向资源对象中存放数据
  10. */
  11. class Input implements Runnable
  12. {
  13. private Res r ;
  14. Object obj = new Object();
  15. Input(Res r)
  16. {
  17.   this.r = r;
  18. }</P>
  19. <P> public void run()
  20. {
  21.   int x = 0;
  22.   while(true)
  23.   {
  24.    synchronized(r)
  25.    {
  26.     if(r.flag)
  27.     try{r.wait();}catch(Exception e){};
  28.     if(x == 0)
  29.     {
  30.      r.name = "mike";
  31.      r.sex = "male";
  32.      x = 1;
  33.     }else{
  34.      r.name = "丽丽";
  35.      r.sex = "女女女女女";
  36.      x = 0;
  37.     }
  38.     r.flag = true ;
  39.     notify();
  40.    }
  41.   }
  42. }
  43. }
  44. /*
  45. Output此线程负责
  46. 从资源对象中取出数据并打印
  47. */
  48. class Output implements Runnable
  49. {
  50. private Res r ;</P>
  51. <P> Output(Res r)
  52. {
  53.   this.r = r;
  54. }
  55. public void run()
  56. {
  57.   while(true)
  58.   {
  59.    synchronized(r)
  60.    {
  61.     if(!r.flag)
  62.     try{r.wait();}catch(Exception e){};
  63.     System.out.println(r.name + "..." + r.sex);
  64.     r.flag = false;
  65.     notify();
  66.    }
  67.   }
  68. }
  69.   
  70. }
  71. /*
  72. 演示线程同步及线程通信
  73. */
  74. class InputOutputDemo
  75. {
  76. public static void main(String[] args)
  77. {
  78.   //创建资源对象 Input和Output线程共享此资源
  79.   Res r = new Res();
  80.   
  81.   Input in = new Input(r);</P>
  82. <P>  Output out = new Output(r);</P>
  83. <P>  //创建线程t1和t2 并分别启动
  84.   Thread t1 = new Thread(in);
  85.   Thread t2 = new Thread(out);</P>
  86. <P>  t1.start();
  87.   t2.start();</P>
  88. <P>
  89. }
  90. }</P>
复制代码
本人对线程中的wait()和notify()方法不太理解,我的这段代码可以编译但不可以执行,出现异常如下!谁能帮忙看看指点一下!

thread.jpg (62.36 KB, 下载次数: 42)

thread.jpg

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

2 个回复

倒序浏览
看一下官方的解释
void notify() :唤醒在此对象监视器上等待的单个线程
void notifyAll() :唤醒在此对象监视器上等待的所有线程
void wait(): 导致当前的线程等待,直到其他线程调用此对象的notify()方法或notiyAll()方法
49.class Output implements Runnable

50.{

51. private Res r ;</P>

52.<P> Output(Res r)

53. {

54.  this.r = r;

55. }

56. public void run()

57. {

58.  while(true)

59.  {

60.   synchronized(r)

61.   {

62.    if(!r.flag)

63.    try{r.wait();}catch(Exception e){};

64.    System.out.println(r.name + "..." + r.sex);

65.    r.flag = false; //把这改成true

66.    notify();

67.   }

68.  }

69. }
回复 使用道具 举报
  1. package cn.javastudy.demo1;

  2. class Res
  3. {
  4. String name;
  5. String sex;
  6. boolean flag = false;
  7. }
  8. /*
  9. Input线程负责
  10. 向资源对象中存放数据
  11. */
  12. class Input implements Runnable
  13. {
  14. private Res r ;
  15. Object obj = new Object();
  16. Input(Res r)
  17. {
  18.   this.r = r;
  19. }
  20. public void run()
  21. {
  22.   int x = 0;
  23.   while(true)
  24.   {
  25.    synchronized(r)
  26.    {
  27.     if(r.flag)
  28.     try{r.wait();}catch(Exception e){};
  29.     if(x == 0)
  30.     {
  31.      r.name = "mike";
  32.      r.sex = "male";
  33.      x = 1;
  34.     }else{
  35.      r.name = "丽丽";
  36.      r.sex = "女女女女女";
  37.      x = 0;
  38.     }
  39.     r.flag = true ;
  40.     r.notify();
  41.    }
  42.   }
  43. }
  44. }
  45. /*
  46. Output此线程负责
  47. 从资源对象中取出数据并打印
  48. */
  49. class Output implements Runnable
  50. {
  51. private Res r ;
  52. Output(Res r)
  53. {
  54.   this.r = r;
  55. }
  56. public void run()
  57. {
  58.   while(true)
  59.   {
  60.    synchronized(r)
  61.    {
  62.     if(!r.flag)
  63.     try{r.wait();}catch(Exception e){};
  64.     System.out.println(r.name + "..." + r.sex);
  65.     r.flag = false;
  66.     r.notify();
  67.    }
  68.   }
  69. }
  70.   
  71. }
  72. /*
  73. 演示线程同步及线程通信
  74. */
  75. class InputOutputDemo
  76. {
  77. public static void main(String[] args)
  78. {
  79.   //创建资源对象 Input和Output线程共享此资源
  80.   Res r = new Res();
  81.   
  82.   Input in = new Input(r);
  83.   Output out = new Output(r);
  84.   //创建线程t1和t2 并分别启动
  85.   Thread t1 = new Thread(in);
  86.   Thread t2 = new Thread(out);
  87.   t1.start();
  88.   t2.start();

  89. }
  90. }
复制代码
你的notify()不是对应锁的监视器,而是this的监视器,所以会发生错误.我给你的这段代码才是正确的,应该用r.notify(),因为你的锁是r对象,且你用r的wait()

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马