练习——管程法,信号灯法简单的实现生产者消费者模型

package com.thread_;

//管程法解决生产者消费者模型
public class PC1 {
    public static void main(String[] args) {

        SynContainer container = new SynContainer();

        Productor productor = new Productor(container);
        Consumer consumer = new Consumer(container);
        productor.start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        consumer.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++) {
            System.out.println("生产了第" + i + "个产品");
            container.pP(new Product(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.cC().id + "件产品");
        }
    }
}
//产品
class  Product {
    int id ;

    public Product(int id) {
        this.id = id;
    }
}
//缓存区
class SynContainer{

    //容器
    Product[] products = new Product[10];
    //计数器
    int count = 0;

    //生产者放入产品
    public synchronized void pP(Product product){
        //如果容器满,需要等待消费
        if (count == products.length){
            //通知消费者消费.生产等待
            System.out.println("等待消费!");
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //如果没满,放入产品
        products[count] = product;
        count++;
        //通知消费者消费
        this.notifyAll();
    }

    //消费者消费产品
    public synchronized Product cC(){
        //判断能否消费
        if (count == 0){
            //等待生产
            System.out.println("等待生产!");
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //消费
        count--;
        Product product = products[count];
        //消费结束,通知生产者生产
        this.notifyAll();
        return product;

    }

}

package com.thread_;

//信号灯法解决生产者消费者模型
public class PC2 {
    public static void main(String[] args) throws Exception{
        Product01 product01 = new Product01();
        new Productor01(product01).start();
        new Consumer01(product01).start();

    }
}

//生产者
class Productor01 extends Thread{
    Product01 product01 ;


    public Productor01(Product01 product01){
        this.product01 = product01;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            if (i%2 == 0){
                this.product01.pP01("生产了一盒饼干");
            }else {
                this.product01.pP01("生产了一盒牛奶");
            }
        }
    }
}

//消费者
class Consumer01 extends  Thread{
    Product01 product01 ;
    public Consumer01(Product01 product01){
        this.product01 = product01;
    }

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


//产品
class Product01 {
    //生产者生产,消费者等待   T
    //消费者消费,生产者等待   F
    String production;  //产品
    boolean flag = true;

    //生产
    public synchronized void pP01(String production){
        if (!flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("生产者生产了" + production);
        //通知消费者消费
        this.notifyAll();
        this.production = production;
        this.flag = !flag;
    }

    //消费
    public synchronized void cC01(){
        if (flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("消费者消费" + production);
        //通知生产者生产
        this.notifyAll();
        this.flag = !flag;

    }

}
posted @ 2023-04-29 19:52  Q1uuuu  阅读(37)  评论(0)    收藏  举报