黑马程序员技术交流社区

标题: 线程间通信时候,遇到个this的问题 [打印本页]

作者: 插兜    时间: 2015-9-17 17:06
标题: 线程间通信时候,遇到个this的问题
本帖最后由 插兜 于 2015-9-17 17:07 编辑

在两条System.out.println那里的name本来都应该是this.name的,然后忘记加this了。打印结果如图
为什么消费者有编号,消费者那里的name也没加this啊。求大神帮忙解惑。
  1. class Resource
  2. {
  3.         private String name;
  4.         private int count=1;
  5.         private boolean flag=false;
  6.         public synchronized void set(String name)
  7.         {
  8.                 while(flag)
  9.                 {
  10.                         try
  11.                         {
  12.                                 wait();
  13.                         }
  14.                         catch (Exception e)
  15.                         {
  16.                         }
  17.                 }
  18.                 this.name=name+"--"+count++;
  19.                 System.out.println(Thread.currentThread().getName()+"...生产者..."+name);
  20.                
  21.                 flag=true;
  22.                 this.notifyAll();
  23.         }
  24.         public synchronized void out()
  25.         {
  26.                 while(!flag)
  27.                 {
  28.                         try
  29.                         {
  30.                                 wait();
  31.                         }
  32.                         catch (Exception e)
  33.                         {
  34.                         }
  35.                 }

  36.                 System.out.println(Thread.currentThread().getName()+"........消费者...."+name);
  37.                 flag=false;
  38.                 this.notifyAll();
  39.         }
  40. }
复制代码








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