一、问题
有n只蜜蜂和一只饥饿的熊。他们共享一罐蜂蜜。这个罐子最初是空的;它的容量是蜜蜂的一半。熊睡到罐子里的蜂蜜满了,然后把所有的蜂蜜都吃了,然后再继续睡觉。每只蜜蜂都要反复地收集一份蜂蜜,并把它放进罐子里蜜蜂填满罐子,唤醒了熊。
二、代码
public class BearAndHoneybees {
//罐子
public static class Pot {
private int start = 0;
private int Max;
private Pot(int n){
Max = n/2;
}
}
//蜜蜂
public static class Bee extends Thread{
private Pot pot;
public Bee(Pot pot){
this.pot=pot;
}
@Override
public void run() {
for (;;)
try {
synchronized (pot) {
while (pot.start == pot.Max) {
try {
pot.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
pot.start++;
pot.notifyAll();
}
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//熊
public static class Bear extends Thread{
private Pot pot;
public Bear(Pot pot){
this.pot=pot;
}
@Override
public