黑马程序员技术交流社区
标题:
线程间通信时候,遇到个this的问题
[打印本页]
作者:
插兜
时间:
2015-9-17 17:06
标题:
线程间通信时候,遇到个this的问题
本帖最后由 插兜 于 2015-9-17 17:07 编辑
在两条System.out.println那里的name本来都应该是this.name的,然后忘记加this了。打印结果如图
QQ截图20150917170132.jpg
(77.69 KB, 下载次数: 6)
下载附件
2015-9-17 17:04 上传
为什么消费者有编号,消费者那里的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();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2