Java实现生产者消费者模型

Java实现生产者消费者模型

最近学习Java多线程操作,记录一下怎么实现其中的生产者消费者模型。

主要是对synchronized,wait(),notifyAll()的使用,保证生产者线程和消费者线程的同步和互斥。

实现的为2个生产者和3个消费者的情况。

关于生产者消费者模型简述

简单来说就是生产者生产产品放入缓冲区,消费者从缓冲区中消费产品,当缓冲区被放满后,生产者不能继续放产品进去,需要等待消费者消费;同样,当缓冲区为空时,消费者也无法消费产品,需要等待生产者生产产品放入。

实现代码

public class ProducerCustomer {
    public static void main(String[] args) {
        SynContainer synContainer = new SynContainer();

        Producer producer1 = new Producer(synContainer);
        Producer producer2 = new Producer(synContainer);
        Customer customer1 = new Customer(synContainer);
        Customer customer2 = new Customer(synContainer);
        Customer customer3 = new Customer(synContainer);
        producer1.setName("p-1");
        producer2.setName(("p-2"));
        customer1.setName("tom");
        customer2.setName("jack");
        customer3.setName("mary");

        producer1.start();
        producer2.start();

        customer1.start();
        customer2.start();
        customer3.start();
    }
}

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

    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            synContainer.push(new Product(i,Thread.currentThread().getName()));
        }
    }
}

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

    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            synContainer.take();
        }
    }
}

// 产品
class Product{
    int id;
    String name;// 生产此产品的生产者的名字
    public Product(int id, String name){
        this.id = id;
        this.name = name;
    }
}

// 缓冲区
class SynContainer{
    // 缓冲区数组
    Product[] products = new Product[10];
    // 缓冲区计数器,判断缓冲区是否已满
    int count = 0;

    // 生产者放入缓冲区
    public synchronized void push(Product product){

        // 如果缓冲区满了,则等待消费者消费
            while(count >= products.length){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            // 如果缓冲区未满,则生产产品
            products[count] = product;
            System.out.println(Thread.currentThread().getName()+"生产了第"+product.id+"个产品");
            count++;

            // 通知消费者消费
            this.notifyAll();

    }

    // 消费者从缓冲区取走产品
    public synchronized void take(){

        // 如果缓冲区为空,则等待生产者放入产品
        while(count <= 0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 缓冲区不为空,则消费产品
        count--;
        Product product = products[count];
        System.out.println(Thread.currentThread().getName()+"消费了"+product.name+"生产的"+"第"+product.id+"个产品");

        // 通知生产者生产
        this.notifyAll();
    }
}

结果

代码结果

(三玖天下第一)

分析代码

创建了四个类,分别为Producer,Customer,Product,SynContainer。在Producer和Customer里要对同一个缓冲区对象synContainer进行操作,故而SynContainer里的push(生产者生产方法)和take(消费者消费方法)方法前都要添加synchronized,以保证对缓冲区对象操作的时候能加锁互斥。

在push和take中,每次都要对缓冲区容量进行判断,并且必须使用while循环判断,如果用if语句的话,线程被唤醒后是不会再判断缓冲区容量,进而若有多个消费者和生产者时,会对count进行脏处理,造成缓冲区数组下标越界;当生产者发现缓冲区已满时,使用wait()方法阻塞自己,此方法会去掉其对缓冲区的锁,未满时放入产品并使用notifyAll()方法来通知所有被阻塞的消费者消费,消费者同理。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值