本帖最后由 冒牌高手 于 2014-8-14 15:06 编辑
源代码如下,错误提示在图片:
class ProCon
{
public static void main(String[] args)
{
//System.out.println("Hello World!");
Resource r=new Resource ();
Pro p=new Pro(r);
Con c=new Con(r);
Thread t0=new Thread(p);
Thread t1=new Thread(c);
t0.start();
t1.start();
}
}
class Resource
{
private String name;
private int num=1;
private boolean flag=false;
public synchronized void set(String name)
{
if (this.flag)
{
try{this.wait();}catch (InterruptedException i){}
}
this.name=name+num;
num++;
System.out.println(Thread.currentThread().getName()+"..生产者...."+this.name);
this.flag=true;
notify();
}
public synchronized void get()
{
if (!this.flag)
{
try{this.wait();}catch (InterruptedException i){}
}
System.out.println(Thread.currentThread().getName()+"..消费者........."+this.name);
this.flag=false;
notify();
}
}
class Pro implements Runnable
{
Resource r;
Pro (Resource r)
{
this.r=r;
}
public void run()
{
while (true)
{
r.set("烤鸭");
}
}
}
class Con implements Runnable
{
Resource r;
Con (Resource r)
{
this.r=r;
}
public void run()
{
//r.get();
while (true)
{
r.get();
}
}
}
|
-
捕获.PNG
(6.52 KB, 下载次数: 17)
怪异的错误提示,计算机还滴滴响
|