class Resource
{
private String name;
private int num=1;
boolean flag=false;
public synchronized void setName(String name)
{
if(flag)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
}
}
this.name=name+num;
num++;
System.out.println(Thread.currentThread().getName()+"..生产.."+this.name);
flag=true;
this.notify();
}
public synchronized void getName()
{
if(!flag)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
}
}
System.out.println(Thread.currentThread().getName()+"..销售.."+this.name);
flag=false;
this.notify();
}
}
在上面的代码上,getName方法里为什么能用this.name?