黑马程序员技术交流社区

标题: 多线程的等待唤醒机制小问题 [打印本页]

作者: 孟子飞宏    时间: 2015-1-13 08:09
标题: 多线程的等待唤醒机制小问题
本帖最后由 孟子飞宏 于 2015-1-13 08:11 编辑
  1. 我写下面错误的代码的结果看图1<div class="blockcode"><blockquote>/*
  2. 线程间通讯:
  3. 多个线程在处理同一资源,但是任务却不同。


  4. */
  5. class Resource
  6. {
  7.         String name;
  8.         String sex;
  9.         boolean flag = false;
  10. }
  11. class Input implements Runnable
  12. {
  13.         Resource r;
  14.         Input(Resource r)
  15.         {
  16.                 this.r = r;
  17.         }
  18.         public void run()
  19.         {
  20.                 int x = 0;
  21.                 while(true)
  22.                 {
  23.                         synchronized(r)
  24.                         {
  25.                                 if(r.flag)
  26.                                 {
  27.                                         try
  28.                                         {
  29.                                                 r.wait();
  30.                                         }
  31.                                         catch (InterruptedException e)
  32.                                         {
  33.                                         }
  34.                                 }
  35.                                 else
  36.                                 {
  37.                                         if(x==0)
  38.                                         {
  39.                                                 r.name = "mike";
  40.                                                 r.sex = "nan";
  41.                                         }
  42.                                         else
  43.                                         {
  44.                                                 r.name = "丽丽";
  45.                                                 r.sex = "女";
  46.                                         }
  47.                                        
  48.                                        
  49.                                         r.notify();
  50.                                         r.flag = true;
  51.                                 }
  52.                         }
  53.                         x=(x+1)%2;
  54.                 }
  55.                
  56.                
  57.         }
  58. }
  59. class Output implements Runnable
  60. {
  61.         Resource r;
  62.         Output(Resource r)
  63.         {
  64.                 this.r = r;
  65.         }
  66.         public void run()
  67.         {
  68.                 while(true)
  69.                 {
  70.                         synchronized(r)
  71.                         {
  72.                                 if(!r.flag)
  73.                                 {
  74.                                         try
  75.                                         {
  76.                                                 r.wait();
  77.                                         }
  78.                                         catch (InterruptedException e)
  79.                                         {
  80.                                         }
  81.                                 }
  82.                                 {
  83.                                         System.out.println(r.name+"..."+r.sex);
  84.                                        
  85.                                         r.notify();
  86.                                         r.flag = false;
  87.                                 }
  88.                                 
  89.                         }
  90.                 }
  91.         }
  92. }
  93. class ResourceDemo2
  94. {
  95.         public static void main(String[] args)
  96.         {
  97.                 //创建资源
  98.                 Resource r = new Resource();
  99.                 //创建任务
  100.                 Input in = new Input(r);
  101.                 Output out = new Output(r);
  102.                 //创建线程
  103.                 Thread t1 = new Thread(in);
  104.                 Thread t2 = new Thread(out);
  105.                 //开启线程
  106.                 t1.start();
  107.                 t2.start();

  108.         }
  109. }
复制代码

正确代码:
  1. /*
  2. 线程间通讯:
  3. 多个线程在处理同一资源,但是任务却不同。


  4. */
  5. class Resource
  6. {
  7.         String name;
  8.         String sex;
  9.         boolean flag = false;
  10. }
  11. class Input implements Runnable
  12. {
  13.         Resource r;
  14.         Input(Resource r)
  15.         {
  16.                 this.r = r;
  17.         }
  18.         public void run()
  19.         {
  20.                 int x = 0;
  21.                 while(true)
  22.                 {
  23.                         synchronized(r)
  24.                         {
  25.                                 if(r.flag)
  26.                                 {
  27.                                         try
  28.                                         {
  29.                                                 r.wait();
  30.                                         }
  31.                                         catch (InterruptedException e)
  32.                                         {
  33.                                         }
  34.                                 }
  35. //                                else
  36. //                                {
  37.                                         if(x==0)
  38.                                         {
  39.                                                 r.name = "mike";
  40.                                                 r.sex = "nan";
  41.                                         }
  42.                                         else
  43.                                         {
  44.                                                 r.name = "丽丽";
  45.                                                 r.sex = "女";
  46.                                         }
  47.                                        
  48.                                        
  49.                                         r.notify();
  50.                                         r.flag = true;
  51. //                                }
  52.                         }
  53.                         x=(x+1)%2;
  54.                 }
  55.                
  56.                
  57.         }
  58. }
  59. class Output implements Runnable
  60. {
  61.         Resource r;
  62.         Output(Resource r)
  63.         {
  64.                 this.r = r;
  65.         }
  66.         public void run()
  67.         {
  68.                 while(true)
  69.                 {
  70.                         synchronized(r)
  71.                         {
  72.                                 if(!r.flag)
  73.                                 {
  74.                                         try
  75.                                         {
  76.                                                 r.wait();
  77.                                         }
  78.                                         catch (InterruptedException e)
  79.                                         {
  80.                                         }
  81.                                 }
  82.                                 {
  83.                                         System.out.println(r.name+"..."+r.sex);
  84.                                        
  85.                                         r.notify();
  86.                                         r.flag = false;
  87.                                 }
  88.                                
  89.                         }
  90.                 }
  91.         }
  92. }
  93. class ResourceDemo2
  94. {
  95.         public static void main(String[] args)
  96.         {
  97.                 //创建资源
  98.                 Resource r = new Resource();
  99.                 //创建任务
  100.                 Input in = new Input(r);
  101.                 Output out = new Output(r);
  102.                 //创建线程
  103.                 Thread t1 = new Thread(in);
  104.                 Thread t2 = new Thread(out);
  105.                 //开启线程
  106.                 t1.start();
  107.                 t2.start();

  108.         }
  109. }
复制代码




错误.PNG (48.16 KB, 下载次数: 0)

图1

图1

正确.PNG (23.23 KB, 下载次数: 0)

正确.PNG

作者: 孟子飞宏    时间: 2015-1-13 08:12
也就37,38,53行把else注释就正确了,不知道为什么




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