本帖最后由 插兜 于 2015-9-17 17:07 编辑
在两条System.out.println那里的name本来都应该是this.name的,然后忘记加this了。打印结果如图
为什么消费者有编号,消费者那里的name也没加this啊。求大神帮忙解惑。
- class Resource
- {
- private String name;
- private int count=1;
- private boolean flag=false;
- public synchronized void set(String name)
- {
- while(flag)
- {
- try
- {
- wait();
- }
- catch (Exception e)
- {
- }
- }
- this.name=name+"--"+count++;
- System.out.println(Thread.currentThread().getName()+"...生产者..."+name);
-
- flag=true;
- this.notifyAll();
- }
- public synchronized void out()
- {
- while(!flag)
- {
- try
- {
- wait();
- }
- catch (Exception e)
- {
- }
- }
- System.out.println(Thread.currentThread().getName()+"........消费者...."+name);
- flag=false;
- this.notifyAll();
- }
- }
复制代码
|
|