生产者消费者模式案例

生产者消费者模式代码案例

package cn.itcast.day0827.java线程安全;
//测试 生产者消费者模型  利用缓冲区解决
//
public class TestPC {
    public static void main(String[] args) {
        SynContainer container = new SynContainer();
        new Productor(container).start();
        new Consumer(container).start();

    }
}

//生产者
class Productor extends Thread{
    SynContainer container;
    public Productor(SynContainer container) {
        this.container = container;
    }

    //生产
    @Override
    public void run(){
        for (int i = 0; i < 100; i++) {

            container.push(new Chicken(i));
            System.out.println("正在生产第"+i+"只小鸡");
        }
    }

}
//消费者
class Consumer extends Thread{
    SynContainer container;
    public Consumer(SynContainer container) {
        this.container = container;
    }

    //消费
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("消费了第"+container.pop().id+"只鸡");
        }
    }
}
//小鸡
class Chicken{
    int id;
    public Chicken(int id){
        this.id=id;
    }
}

//缓冲区
class SynContainer{

    int count;

    Chicken[] chickens = new Chicken[10];

    //生产者放入产品
    public synchronized void push(Chicken chicken){
        //容器满了等待消费者消费
        if(count==chickens.length){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //未满的状态 需要放入鸡
        chickens[count]=chicken;
        count++;
        //通知消费者消费
        this.notifyAll();
    }
    //消费者消费
    public synchronized Chicken pop(){
        //判断能否消费
        if(count==0){
            //等待生者生产
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //如果可以消费
        count--;
        Chicken chicken = chickens[count];

        //吃完了通知生产者生产
        return chicken;
    }
}

演员观众代码案例

package cn.itcast.day0827.java线程安全;

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 < 200; i++) {
            if(i%2==0){
                this.tv.play("节目1");
            }else{
                this.tv.play("节目2");
            }
        }
    }
}

//观众类
class Watcher extends Thread{
    TV tv;
    public Watcher(TV tv){
        this.tv=tv;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; 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.notifyAll();
        this.voice=voice;
        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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值