/*public*/ class Shop //定义一个商店类
{
private int breadNumber; //面包的数量
private int breadMaxSize; //商店里起始面包的数量
public int numberInWaitingPool; //shop对象的等待池中等待者的数量
public Shop(int breadMaxSize)
{
this.breadMaxSize=breadMaxSize;
}
public synchronized int getBreadNumber() //对临界资源的访问
{
return this.breadNumber;
}
public synchronized void makeBread()
{
while (breadNumber==breadMaxSize) //商店中的面包已满
{
try {
/*try{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
} */
System.out.print( Thread.currentThread().getName()+"又想做新的面包...但是商店不能再放更多的面包了...所以");
System.out.println( Thread.currentThread().getName()+"被送入shop对象的等待池中.\n");
this.numberInWaitingPool++;
/*try{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
} */
this.wait();
}
catch (InterruptedException e) {}
}
this.breadNumber++; //商店中面还能放面包,生产面包,则面包数量加1
/*try{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
} */
System.out.print(" 商店还有"+this.breadNumber+"个面包.");
if (this.numberInWaitingPool>0) //等待池中有处于等待状态的线程
{
this.notifyAll(); //随机从shop对象的等待池中唤醒一个线程
System.out.print("(商店里面又有面包了,所以从shop对象的等待队列中走出一个线程)\n");
this.numberInWaitingPool--; //等待池中线程数量减1
}
}
public synchronized void eatBread()
{
while(breadNumber==0){ //商店中已经没有面包了
try {
/*try{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
} */
System.out.print( Thread.currentThread().getName()+"又来吃了...但是商店已经没有面包了...所以");
System.out.println(Thread.currentThread().getName()+"被送入shop对象的等待池中.");
this.numberInWaitingPool++;
this.wait();
}
catch (InterruptedException e) {}
}
this.breadNumber--; //商店中若有面包,吃了一块面包后,则其数量减1
/*try{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
} */
System.out.print(" 商店还有"+this.breadNumber+"个面包.");
if (this.numberInWaitingPool>0) //等待池中有处于等待状态的线程
{
this.notifyAll(); //随机从shop对象的等待池中唤醒一个线程
System.out.print("(商店中空间有富余了.从shop对象的等待池中走出一个线程)\n\n");
this.numberInWaitingPool--; //等待池中现成数量减1
}
}
}
/*public*/ class Consumer extends Thread //定义个消费者类
{
private String name;
private Shop shop;
public Consumer(String name,Shop shop)
{
this.shop=shop;
this.name=name;
}
public void run()
{
while(true)
{
shop.eatBread();
System.out.println("因为消费者"+name+"吃掉了一个面包.\n");
}
}
}
public class Producer extends Thread //定义一个生产者类
{
private Shop shop;
private String name;
public Producer(String name,Shop shop)
{
this.name=name;
this.shop =shop;
}
public void run()
{
while(true)
{
shop.makeBread();
System.out.println("因为生产者"+this.name+"做了一个面包.\n");
}
}
public static void main(String[] args) //主过程
{
Shop shop=new Shop(10);
Producer p1=new Producer("A",shop);
Producer p2=new Producer("B",shop);
Consumer c=new Consumer("C",shop);
Consumer d=new Consumer("D",shop);
p1.setName(" 生产者A");
p1.start();
/*try{
Thread.sleep(3000);
}
catch(InterruptedException e)
{
} */
p2.setName(" 生产者B");
p2.start();
/*try{
Thread.sleep(3000);
}
catch(InterruptedException e)
{
} */
c.setName(" 消费者C");
c.start();
/* try{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
} */
d.setName(" 消费者D");
d.start();
/*try{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
} */
}
}