美文网首页
生产者模型和消费者模型

生产者模型和消费者模型

作者: 真胖大海 | 来源:发表于2019-11-10 13:31 被阅读0次
image.png

一.单锁双条件的实现

public class MyBlockingQueue<T> {

    private int mCapacity;
    private LinkedList<T> mTable;

    private ReentrantLock lock = new ReentrantLock();
    private Condition mNotFull = lock.newCondition();
    private Condition mNotEmpty = lock.newCondition();


    public MyBlockingQueue(int mCapacity) {
        this.mCapacity = mCapacity;
        mTable = new LinkedList<>();
    }

    public boolean put(T t) {
        try {
            lock.lock();
            while(mTable.size()==mCapacity){
                mNotFull.await();
            }
            mTable.add(t);
            mNotEmpty.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

        return true;
    }

    public T get() {
        T first=null;
        try {
            lock.lock();
            while(mTable.size()==0){
                mNotEmpty.await();
            }
            first=mTable.removeLast();
            mNotFull.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

        return first;
    }
}

二.双锁双条件的实现

public class MyBlockingQueue<T> {

    private int mCapacity;
    private LinkedList<T> mTable;

    private ReentrantLock mPutLock = new ReentrantLock();
    private ReentrantLock mGetLock = new ReentrantLock();
    private Condition mNotFull = mPutLock.newCondition();
    private Condition mNotEmpty = mGetLock.newCondition();
    private AtomicInteger count=new AtomicInteger(0);

    public MyBlockingQueue(int mCapacity) {
        this.mCapacity = mCapacity;
        mTable = new LinkedList<>();
    }

    public boolean put(T t) {
        mPutLock.lock();
        int c=-1;
        try {
            while (mTable.size() == mCapacity) {//这里使用whie是为了避免线程在条件不满足是被意外唤醒
                mNotFull.await();
            }

            mTable.addLast(t);
            c=count.getAndIncrement();
        } catch (InterruptedException e) {
            return false;
        } finally {
            mPutLock.unlock();
        }
        if (c == 0) {//只有原来数量为0的情况才唤醒在mNotEmpty上等待的线程
            mGetLock.lock();
            mNotEmpty.signal();
            mGetLock.unlock();
        }
        return true;
    }

    public T get() {
        T first=null;
        mGetLock.lock();
        int c=-1;
        try {
            while (mTable.size() == 0) {
                mNotEmpty.await();
            }
             first = mTable.removeFirst();
             c=count.getAndDecrement();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            mGetLock.unlock();
        }
        if (c== mCapacity) {//只有原来数量满了的情况才唤醒在mNotFull上等待的线程
            mPutLock.lock();
            mNotFull.signal();
            mPutLock.unlock();
        }
        return first;
    }
}

三. 两种实现的比较

因为单锁双条件只有一个锁,所以读的时候不能写,写的时候不能读
但是双锁双条件有两个锁,所以读的时候能写,写的时候能读。

相关文章

  • 生产者和消费者模型

    生产者和消费者模型 1. 什么是生产者和消费者模型 生产者消费者模型具体来讲,就是在一个系统中,存在生产者和消费者...

  • 34.Python之生产者消费者模型

    Python之生产者消费者模型(非常重要) 生产者消费者模型模型指的是一种解决问题的套路。 生产者消费者模型中包含...

  • 生产者消费者(一)

    生产者消费者模型: 生产者------> 缓存<-------- 消费者

  • python入门开发学习笔记之掌握什么是生产者消费者模型

    本节重点 熟练掌握什么是生产者消费者模型熟练掌握为什么要用生产者消费者模型熟练掌握如何实现生产者消费者模型本节时长...

  • Future

    Future 模式只是生产者-消费者模型的扩展。经典“生产者-消费者”模型中消息的生产者不关心消费者何时处理完该条...

  • Linux生产者消费者模型与C/C++子线程调用Java

    生产者消费者模型 基于生产者和消费者的模型在编程中运用是较多。生产者是一个或者多个线程产生数据,消费者是另一个或者...

  • wait/notify实现生产者消费者(6)

    生产者消费者模型 生产者消费者模型是一个典型的多线程问题,涉及生产者、消费者、产品仓库。生产者生产的产品放入仓库中...

  • 生产者消费者模型Java实现

    生产者消费者模型 生产者消费者模型可以描述为:①生产者持续生产,直到仓库放满产品,则停止生产进入等待状态;仓库不满...

  • 生产者与消费者模型

    生产者与消费者模型 通过使用Object的wait(),notify()方法进行生产者与消费者模型中出现的数据同步...

  • 生产者消费者

    生产者/消费者模式(阻塞队列) 生产者消费者模型的实现

网友评论

      本文标题:生产者模型和消费者模型

      本文链接:https://www.haomeiwen.com/subject/hrqabctx.html