import java.util.List;
import java.util.Vector;
/**
* 吃桃子程序
* @date 2013-5-4
*/
public class EatPeachMain {
public static void main(String[] args) {
Plate plate = new Plate();
//父亲线程
ParentThread papa = new ParentThread("Papa", plate);
//母亲线程
ParentThread mama = new ParentThread("mama", plate);
//老大, 吃桃子速度为3
ChildThread eldest = new ChildThread("Eldest", plate, 3);
//老二吃桃子速度为2
ChildThread second = new ChildThread("Second", plate, 2);
//老三吃桃子速度为1
ChildThread baby = new ChildThread("baby", plate, 1);
//启动
papa.start();
mama.start();
eldest.start();
second.start();
baby.start();
}
}
/**
* 桃子, 模拟
* @date 2013-5-4
*/
class Peach{
}
/**
* 盘子, 临界区
* @date 2013-5-4
*/
class Plate{
//定义盘子容量
private static final int capacity = 5;
//当前盘里的桃子数量
private volatile int currentPeaches;
//实际上存放桃子的容器
private Listpeaches;
//互斥锁
private byte[] mutex;
public Plate(){
this.peaches = new Vector();
mutex = new byte[0];
}
/**
* 放一个桃子到盘子里
* @param peach
* @throws InterruptedException
*/
public void put(Peach peach) throws InterruptedException{
synchronized (mutex) {
//如果盘子满了就一直等待
while(currentPeaches == capacity){
mutex.wait();
}
currentPeaches++;
this.peaches.add(peach);
//放过桃子, 说明盘子不空了, 可以吃了
mutex.notifyAll();
}
}
public void reduce(int speed) throws InterruptedException{
synchronized (mutex) {
//如果盘子空了, 就一直等待
while(currentPeaches == 0){
mutex.wait();
}
//吃桃子就是把桃子从容器里拿走, 如果速度大于现有桃子, 则吃完了就不用等了
while(peaches.size()>0 && speed > 0){
peaches.remove(0);
speed--;
currentPeaches -- ;
}
//吃过桃子后说明盘子不满了, 可以继续放
mutex.notifyAll();
}
}
}
/**
* 孩子线程
* @date 2013-5-4
*/
class ChildThread extends Thread{
//孩子的名称
private String name;
//盘子
private Plate plate;
//吃桃子速度
private int speed;
public ChildThread(String name, Plate plate, int speed){
super(name);
this.plate = plate;
this.speed = speed;
this.name = name;
}
@Override
public void run() {
while(true){
try {
eat();
} catch (InterruptedException e) {
}
}
}
private void eat() throws InterruptedException {
//吃桃子就是从盘子里面拿走若干个, 由speed和当前剩余的数量决定
plate.reduce(speed);
System.out.println(name + " : I eat peach(es)... speed=" + speed);
//吃完桃子休息一下
sleep(speed * 1000);
}
}
/**
* 父母线程
* @date 2013-5-4
*/
class ParentThread extends Thread{
//名称
private String name;
//盘子
private Plate plate;
public ParentThread(String name, Plate plate){
super(name);
this.plate =plate;
this.name = name;
}
@Override
public void run() {
while(true){
try {
put();
} catch (InterruptedException e) {
}
}
}
/**
* 往盘子里放桃子
* @throws InterruptedException
*/
private void put() throws InterruptedException {
plate.put(new Peach());
System.out.println(name + " : I put a peach");
}
}
源代码和注释都给你了, 可以直接在IDE里面跑起来的.
运行结果部分输出如下:
Papa : I put a peach
mama : I put a peach
baby : I eat peach(es)... speed=1
Second : I eat peach(es)... speed=2
mama : I put a peach
Eldest : I eat peach(es)... speed=3
Papa : I put a peach
mama : I put a peach
Papa : I put a peach
mama : I put a peach
Papa : I put a peach
baby : I eat peach(es)... speed=1
Papa : I put a peach
baby : I eat peach(es)... speed=1
mama : I put a peach
Papa : I put a peach
Second : I eat peach(es)... speed=2
mama : I put a peach
mama : I put a peach
Papa : I put a peach
baby : I eat peach(es)... speed=1
Eldest : I eat peach(es)... speed=3
Papa : I put a peach
mama : I put a peach
mama : I put a peach
baby : I eat peach(es)... speed=1
Papa : I put a peach
Second : I eat peach(es)... speed=2
mama : I put a peach
baby : I eat peach(es)... speed=1
mama : I put a peach
Second : I eat peach(es)... speed=2
mama : I put a peach
Papa : I put a peach
Papa : I put a peach
baby : I eat peach(es)... speed=1
Papa : I put a peach
Papa : I put a peach
mama : I put a peach
Eldest : I eat peach(es)... speed=3