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

本帖最后由 插兜 于 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. }
复制代码



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马