线程经典问题 生产者和消费者

package 线程;

/**
 * 生产者和消费者的问题 两种情况 一个生产者和一个消费者 多个生产者和多个消费者
 *
 * 代码描述 一个 egg 类表示生产者 和消费者 生产或消费的 蛋 一个 eggbox 类表示 装蛋 的容器 有固定大小 一个生产者类 一个消费者类
 * 生产者不停的生产蛋, eggbox 满了就停止生产 消费者不停的消费但, eggbox 空调就停止消费
 */
public class ProducerAndConsumer {

    public static void main(String[] args) {
        EggBox eb = new EggBox(9);

        Producers p = new Producers(eb);
        Producers p1 = new Producers(eb);

        Consumers c = new Consumers(eb);
        Consumers c1 = new Consumers(eb);
        new Thread(p).start();
        new Thread(p1).start();

        new Thread(c).start();
        new Thread(c1).start();

    }

}

class Egg {
    public int id;

    public Egg(int id) {
        this.id = id;
    }
}

class EggBox {
    // 表示能放多少个蛋

    public EggBox(int size) {
        eggs = new Egg[size];
    }

    int num = 0; // 数组的索引
    private Egg[] eggs;

    public int getSize() {
        return eggs.length;
    }

    public synchronized void push(Egg g) {
        // 如果蛋的数量满了 就让生产的线程进入等待状态
        while (num >= eggs.length) { // 多个生产者时必须用while,不然 可能几个生产者等在下边,造成数据错乱
            try {
                this.wait();
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        eggs[num++] = g;
        System.out.println("the producer make egg --total--" + num);
        this.notifyAll(); // 多个生产者线程时用 notifyAll() 单个生产者线程时用 notify()
    }

    public synchronized Egg pop() {
        // //如果蛋的数量没了 就让生产的线程进入等待状态
        while (num <= 0) { // 多个消费者时必须用while,不然 可能几个消费者等在下边,造成数据错乱
            try {
                this.wait();
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        Egg egg = eggs[--num];
        System.out.println("the consumer eat the egg-------total---" + num);
        this.notifyAll(); // 多个消费者线程时用 notifyAll() 单个消费者线程时用 notify()
        return egg;

    }
}

class Producers implements Runnable {
    private EggBox eb;

    public Producers(EggBox eb) {
        this.eb = eb;
    }

    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            eb.push(new Egg(i));

            try {
                Thread.sleep((int) Math.random() * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

class Consumers implements Runnable {
    private EggBox eb;

    public Consumers(EggBox eb) {
        this.eb = eb;
    }

    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            Egg egg = eb.pop();

            try {
                Thread.sleep((int) Math.random() * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值