生产者与消费者模型

单生产单消费

商品类

class Goods{
    private String goodsName;//商品名称
    private int count;//商品库存
    //生产方法
    public synchronized void set(String goodsName){
        this.goodsName = goodsName;
        this.count++;
        System.out.println(Thread.currentThread().getName()+"生产"+toString());
    }
    //消费方法
    public synchronized void get(){
        //每次消费一次商品
        this.count--;
        System.out.println(Thread.currentThread().getName()+"消费"+toString());
    }
    @Override
    public String toString() {
        return "Goods{" +
                "goodsName='" + goodsName + '\'' +
                ", count=" + count +
                '}';
    }
}

生产者类

class Producer implements Runnable{
    private Goods goods;
    public Producer(Goods goods) {
        this.goods = goods;
    }
    @Override
    public void run() {
        this.goods.set("生产奔驰C200L");
    }
}

消费者类

class Consumer implements Runnable{
    private Goods goods;
    public Consumer(Goods goods) {
        this.goods = goods;
    }

    @Override
    public void run() {
        this.goods.get();
    }
}

测试类

public class CPTest {
    public static void main(String[] args) throws InterruptedException {
        Goods goods = new Goods();
        Thread producerThread = new Thread(new Producer(goods),"生产者线程");
        Thread consumerThread = new Thread(new Consumer(goods),"消费者线程");
        producerThread.start();
        Thread.sleep(1000);
        consumerThread.start();
    }
}

注意:如果先开启消费者线程后开启生产者线程就会导致count为负数。此时就要用到我们的wait()和notify()。

class Goods{
    private String goodsName;//商品名称
    private int count;//商品库存
    //生产方法
    public synchronized void set(String goodsName) throws InterruptedException {
        if (count>0){
            System.out.println("此时还有商品,一会儿再生产......");
            this.wait();
        }
        this.goodsName = goodsName;
        this.count++;
        System.out.println(Thread.currentThread().getName()+"生产"+toString());
        notify();
    }
    //消费方法
    public synchronized void get() throws InterruptedException {
        if (count==0){
            System.out.println("商品已经售罄,等待货品上架.....");
            this.wait();
        }
        //每次消费一次商品
        this.count--;
        System.out.println(Thread.currentThread().getName()+"消费"+toString());
        notify();
    }
    @Override
    public String toString() {
        return "Goods{" +
                "goodsName='" + goodsName + '\'' +
                ", count=" + count +
                '}';
    }
}

单商品多生产多消费

class Goods{
    private String goodsName;//商品名称
    private int count;//商品库存
    //生产方法
    public synchronized void set(String goodsName) throws InterruptedException {
        while (count>0){
            System.out.println("此时还有商品,一会儿再生产......");
            this.wait();
        }
        this.goodsName = goodsName;
        this.count++;
        System.out.println(Thread.currentThread().getName()+"生产"+toString());
        notifyAll();
    }
    //消费方法
    public synchronized void get() throws InterruptedException {
        while (count==0){
            System.out.println("商品已经售罄,等待货品上架.....");
            this.wait();
        }
        //每次消费一次商品
        this.count--;
        System.out.println(Thread.currentThread().getName()+"消费"+toString());
        notifyAll();
    }
    @Override
    public String toString() {
        return "Goods{" +
                "goodsName='" + goodsName + '\'' +
                ", count=" + count +
                '}';
    }
}
//------------------------------------------------------------------
//生产者类
class Producer implements Runnable{
    private Goods goods;
    public Producer(Goods goods) {
        this.goods = goods;
    }
    @Override
    public void run() {
        while (true){
            try {
                this.goods.set("生产奔驰C200L");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
//------------------------------------------------------------------
//消费者类
class Consumer implements Runnable{
    private Goods goods;
    public Consumer(Goods goods) {
        this.goods = goods;
    }

    @Override
    public void run() {
       while (true){
           try {
               this.goods.get();
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }
    }
}
//-------------------------------------------------------------
//测试类
public class CPTest {
    public static void main(String[] args) throws InterruptedException {
        Goods goods = new Goods();
        Thread producerThread1 = new Thread(new Producer(goods),"生产者线程1");
        Thread producerThread2 = new Thread(new Producer(goods),"生产者线程2");
        Thread producerThread3 = new Thread(new Producer(goods),"生产者线程3");
        Thread producerThread4 = new Thread(new Producer(goods),"生产者线程4");
        Thread consumerThread1 = new Thread(new Consumer(goods),"消费者线程1");
        Thread consumerThread2 = new Thread(new Consumer(goods),"消费者线程2");
        Thread consumerThread3 = new Thread(new Consumer(goods),"消费者线程3");

        consumerThread1.start();
        consumerThread2.start();
        consumerThread3.start();

        producerThread1.start();
        producerThread2.start();
        producerThread3.start();
        producerThread4.start();
    }
}

多商品多生产多消费


class Goods{
    private String goodsName;//商品名称
    private int count;//商品库存
    private int maxCount;

    public Goods(int maxCount) {
        this.maxCount = maxCount;
    }

    //生产方法
    public synchronized void set(String goodsName) throws InterruptedException {
        while (count==maxCount){
            System.out.println("此时还有商品,一会儿再生产......");
            this.wait();
        }
        this.goodsName = goodsName;
        this.count++;
        System.out.println(Thread.currentThread().getName()+"生产"+toString());
        notifyAll();
    }
    //消费方法
    public synchronized void get() throws InterruptedException {
        while (count==0){
            System.out.println("商品已经售罄,等待货品上架.....");
            this.wait();
        }
        //每次消费一次商品
        this.count--;
        System.out.println(Thread.currentThread().getName()+"消费"+toString());
        notifyAll();
    }
    @Override
    public String toString() {
        return "Goods{" +
                "goodsName='" + goodsName + '\'' +
                ", count=" + count +
                '}';
    }
}
//------------------------------------------------------------------
//生产者类
class Producer implements Runnable{
    private Goods goods;
    public Producer(Goods goods) {
        this.goods = goods;
    }
    @Override
    public void run() {
        while (true){
            try {
                this.goods.set("生产奔驰C200L");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
//------------------------------------------------------------------
//消费者类
class Consumer implements Runnable{
    private Goods goods;
    public Consumer(Goods goods) {
        this.goods = goods;
    }

    @Override
    public void run() {
       while (true){
           try {
               this.goods.get();
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }
    }
}
//-------------------------------------------------------------
//测试类
public class CPTest {
    public static void main(String[] args) throws InterruptedException {
        Goods goods = new Goods(10);
        Thread producerThread1 = new Thread(new Producer(goods),"生产者线程1--");
        Thread producerThread2 = new Thread(new Producer(goods),"生产者线程2--");
        Thread producerThread3 = new Thread(new Producer(goods),"生产者线程3--");
        Thread producerThread4 = new Thread(new Producer(goods),"生产者线程4--");
        Thread consumerThread1 = new Thread(new Consumer(goods),"消费者线程1--");
        Thread consumerThread2 = new Thread(new Consumer(goods),"消费者线程2--");
        Thread consumerThread3 = new Thread(new Consumer(goods),"消费者线程3--");

        consumerThread1.start();
        consumerThread2.start();
        consumerThread3.start();

        producerThread1.start();
        producerThread2.start();
        producerThread3.start();
        producerThread4.start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值