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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 rick1991chen 于 2015-4-22 14:17 编辑
  1. class Res
  2. {
  3.          String name;
  4.          String sex;        
  5.          boolean flag = false;
  6. }

  7. class Input implements Runnable
  8. {
  9.         private Res r;
  10.         Input(Res r)
  11.         {
  12.                 this.r = r;
  13.         }
  14.         public void run()
  15.         {
  16.                 int x = 0;
  17.                 while (true)
  18.                 {
  19.                         synchronized(r)
  20.                         {
  21.                                 if (r.flag)
  22.                                         try{r.wait();}catch(Exception e){}
  23.                                 if (x==0)
  24.                                 {
  25.                                         r.name="瑞克";
  26.                                         r.sex="男";
  27.                                 }
  28.                                 else
  29.                                 {
  30.                                         r.name = "Lily";
  31.                                         r.sex = "female";
  32.                                 }
  33.                                 x = (x+1)%2;
  34.                                 r.flag = true;
  35.                                 r.notify();
  36.                         }
  37.                 }
  38.         }
  39. }

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

  62. class InputOutputDemo
  63. {
  64.         public static void main(String[] args)
  65.         {
  66.                 Res r = new Res();
  67.                 new Thread (new Input(r)).start();
  68.                 new Thread (new Output(r)).start();
  69.         }
  70. }
复制代码
这个 程序的问题我知道出在输出函数中的 更改标记的位置。 整个代码的用意是通过等待唤醒交替赋值,打印一个共享数据。但是出现了标记更改错误的时候,打印结果变成了交替打印一排,如图所示:


有没有大神能给分析一下,整个线程的运行过程 (用代码行行数+一点解释的语句)

threadpic2.JPG (34.51 KB, 下载次数: 0)

先出现

先出现

threadpic3.JPG (45.01 KB, 下载次数: 0)

然后又出现的

然后又出现的

9 个回复

倒序浏览
就是第58行   r.flag = true; 这里本来应该是false;赋值为true造成了这个结果,是为什么?
回复 使用道具 举报
沉了么?还是太简单没悬赏没人想回答。。。。
回复 使用道具 举报
顶你!辛苦了~
回复 使用道具 举报

......晕,谢谢
回复 使用道具 举报

根据我这么多天的经验,这种帖子是必沉的,我帮你顶一下~稍等,我看看你的问题,看看我会不会,我会的一定告诉你。
回复 使用道具 举报
这个问题我记得我发帖总结过,你稍等,我找一下,找到了发给你看看,应该就明白了。
回复 使用道具 举报
线程间通信练习 [url]http://bbs.itheima.com/thread-185446-1-1.html (出处: 黑马程序员IT技术论坛)[/url]
你先把代码复制下来,再一边看一边调试吧。我想我这个帖子应该写得很详细了,代码都是可以直接运行的。
回复 使用道具 举报
itheima_llt 发表于 2015-4-22 13:56
线程间通信练习 http://bbs.itheima.com/thread-185446-1-1.html (出处: 黑马程序员IT技术论坛)[/url]
你先 ...

OK 去也 3Q
回复 使用道具 举报
不错不错!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马