基于内置锁的生产者消费者模型

本文深入探讨了生产者消费者模型,该模型中生产者负责填充仓库,而消费者则负责消耗仓库中的资源。通过内置锁机制,确保了生产与消费的同步,防止资源的过度生产和耗尽,实现高效且稳定的系统运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里有三个概念,生产者,消费者,仓库

public class Repository {
    private int capability;//仓库容量
    private boolean stoped=false;//生产者线程是否退出的标志,如果生产者线程退出,则消费者线程消费完所以商品后也退出,而不是等待
    private List<String> list=new ArrayList<>();
    public Repository(int capability){
        this.capability=capability;
    }
    public synchronized void add(String product) throws InterruptedException {
        list.add(product);
    }
    public synchronized String remove() throws InterruptedException {
        return list.remove(0);
    }
    public boolean isFull(){
        return list.size()==capability;
    }
    public boolean isEmpty(){
        return list.size()==0;
    }
    public void stop(){
        stoped=true;
    }
    public boolean isStoped(){
        return stoped;
    }
}

public class Productor extends Thread {
    private Repository repository;
    private int i=1;
    public Productor(Repository repository){
        this.repository=repository;
    }
    @Override
    public void run(){
        while (i<1000){
            synchronized (repository){
                try {
                    if(!repository.isFull()){
                        Thread.sleep(100);
                        String product=Thread.currentThread().getName()+(i++);
                        System.out.println("生产商品:"+product);
                        repository.add(product);
                        repository.notifyAll();
                    }else {
                        System.out.println("仓库已满,暂停生产");
//                        repository.notifyAll();如果notifyAll放在此次,则只有仓库满的时候会通知消费者
                        repository.wait();
                    }
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        repository.stop();
    }
}

public class Consumer extends Thread {
    private Repository repository;
    public Consumer(Repository repository){
        this.repository=repository;
    }
    @Override
    public void run(){
        boolean stoped=false;
        while (!stoped){
            synchronized (repository){
                try {
                    if(!repository.isEmpty()){
                        System.out.println("消费商品:"+repository.remove());
                        repository.notifyAll();
                    }else {
                        System.out.println("仓库已空,暂停消费");
                        if(repository.isStoped()){
                            stoped=true;
                        }else{
//                            repository.notifyAll();如果notifyAll放在此次,则只有仓库空的时候会通知生产者者
                            repository.wait();
                        }
                    }
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Repository repository=new Repository(100);
        new Productor(repository).start();
        Thread.sleep(10000);
        new Consumer(repository).start();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值