帮忙看下 下面标红的是命令行里面提示的问题描述,怎么解决啊?
class Demo2
{
public static void main(String[] args)
{
ZiYuan zy=new ZiYuan();
XiaoFei xf=new XiaoFei(zy);
ShengChan sc=new ShengChan(zy);
Thread th1=new Thread(xf);
Thread th2=new Thread(sc);
th1.start();
th2.start();
}
}
class ZiYuan
{
private String name;
private int Num1=1;
private int Num2=2;
private boolean flag;
public synchronized void XF(String name)
{
if(flag)
{
try{this.wait();}catch(InterruptedException e){}
}
this.name=name;
System.out.println(Thread.currentThread().getName()+"生产产品"+this.name+(Num1++));
flag=true;
this.notify();
}
public synchronized void SC(String name)
{
if(!flag)
{
try{this.wait();}catch(InterruptedException e){}
}
System.out.println(Thread.currentThread().getName()+"消费产品"+this.name+(Num2++));
flag=false;
this.notify();
}
}
class XiaoFei implements Runnable
{
private ZiYuan r;
public XiaoFei(ZiYuan r)
{
this.r=r;
}
public void run()
{
while (true)
{
r.XF("馒头");
}
}
}
class ShengChan implements Runnable
{
private ZiYuan r;
public ShengChan(ZiYuan r)
{
this.r=r;
}
public void run()
{
while (true)
{
r.SC();//提示无法将ZiYuan 中的 SC<java.lang.String>应用于<> r.SC();
}
}
}
|