JAVA多线程9——生产者消费者问题

一、管程法——通过缓冲区解决

public class TestPC {
    public static void main(String[] args) {
        SyContainer syContainer = new SyContainer();
        new Productor(syContainer).start();
        new Consumer(syContainer).start();
    }
}
class Productor extends Thread{
    //容器,及缓冲区
    SyContainer container;
    public Productor(SyContainer container){
        this.container = container;
    }
    //生产100知鸡
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("生产鸡的编号"+i);
            container.push(new Chicken(i));
        }
    }
}
class Consumer extends Thread{
    //容器,及缓冲区
    SyContainer container;
    public Consumer(SyContainer container){
        this.container = container;
    }

    @Override
    public void run() {
        for (int j = 0; j < 10; j++) {
            System.out.println("消费了鸡的编号->"+container.pull().id);
        }
    }
}

class Chicken {
    int id;//产品编号

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

//缓冲区
class SyContainer {
    //设置一个容器大小
    Chicken[] chickens = new Chicken[10];
    int count = 0;  //容器计数器
    //生产者放入产品
    public synchronized void push(Chicken newc){
        if(chickens.length==count){
            //通知消费者消费,缓冲器已满
            //生产登台
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //如果没有满,我们就需要生成商品
        chickens[count]=newc;
        count++;

        //通知消费者可以消费了
        this.notifyAll();
    }

    //消费者消费产品

    public synchronized Chicken pull(){
        if(count==0){
            //消费者等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //消费
        count--;
        Chicken eatc = chickens[count];

        this.notifyAll();
        return eatc;
    }
}

二、信号灯法——通过标志位解决

public class TestPC2 {
    public static void main(String[] args) {
        TV tv = new TV();
        new Player(tv).start();
        new Watcher(tv).start();
    }
}

//演员
class Player extends Thread{
    TV tv;
    public Player(TV tv){
        this.tv=tv;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            if(i%2==0){
                this.tv.play("节目");
            }
            else {
                this.tv.play("广告");
            }
        }
    }
}
//观众
class Watcher extends Thread{
    TV tv;
    public Watcher(TV tv){
        this.tv=tv;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            tv.watch();
        }
    }
}

class TV{
    String voice;//节目
    boolean flag = true;

    //表演
    public synchronized void play(String voice){
        if (!flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("演员表演了-》"+voice);
        this.voice = voice;
        this.notifyAll();
        this.flag = !this.flag;//新节目已出
    }
    //观看
    public synchronized void watch(){
        if (flag) {
            try {
                this.wait();//等待
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("观众观看了-》" + voice);
        this.notifyAll();
        this.flag=!this.flag;//节目已经被观看
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上兵伐眸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值