欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

生产者消费者 Lock Condition实现

程序员文章站 2022-07-12 18:39:27
...

    网上有很多生产者消费者模型,很多都是基于synchronized, wait, notify的,也有基于Lock的,下面是我用Lock, Condition实现的.

 

1 仓库:

 

class GodOwn {
    //声明锁对象
    private Lock lock = new ReentrantLock();

    //当仓库中的物品数量不够消费的条件
    Condition lessCondition = lock.newCondition();

    //当仓库中的物品数量超过仓库的总量的条件
    Condition moreCondition = lock.newCondition();

    private final int max_size = 50;

    private int baseNum = 0;

    int getMax_size() {
        return max_size;
    }

    int getBaseNum() {
        return baseNum;
    }

    void setBaseNum(int baseNum) {
        this.baseNum = baseNum;
    }

    public void produce (int neednum) {
        lock.lock();
        try {
            while (this.getBaseNum() + neednum > this.getMax_size()) {//超过总量不生产
                try {
                    moreCondition.await();//超过仓库总量时,生产线程等待
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.setBaseNum(this.getBaseNum() + neednum);
            System.out.println("生产了" + neednum + "个,仓库共有" + this.getBaseNum() + "个!");
            moreCondition.signalAll();//本轮生产完毕,唤醒对象上所有的线程,看是否需要生产还是消费
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();//释放锁,这一步很重要,最好在finally语句块里执行,防止程序异常,锁永远无法释放
        }
    }

    public void reduce(int neednum) {
        lock.lock();
        try {
            while (this.getBaseNum() - neednum < 0) {
                try {
                    lessCondition.await();//仓库总量不够本次消费数量时,消费线程等待
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.setBaseNum(this.getBaseNum() - neednum);
            System.out.println("消费了" + neednum + "个,仓库共有" + this.getBaseNum() + "个!");
            lessCondition.signalAll();//本轮消费完毕,唤醒对象上所有的线程,看是否需要生产还是消费
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();//释放锁
        }
    }

}

 

 

2 生产者模型

  

class ProduceThread extends Thread {

    private GodOwn godOwn;

    private int neednum;

    ProduceThread(GodOwn godOwn, int neednum) {
        this.godOwn = godOwn;
        this.neednum = neednum;
    }

    public void run() {
        godOwn.produce(neednum);
    }

}

 3 消费者模型

 

 

class ResuseThread extends Thread {
    private GodOwn godOwn;

    private int neednum;

    ResuseThread(GodOwn godOwn, int neednum) {
        this.godOwn = godOwn;
        this.neednum = neednum;
    }

    public void run () {
        this.godOwn.reduce(neednum);
    }
}

 

 

4 测试代码

 

    public static void main(String[] args) {
        GodOwn godOwn = new GodOwn();
        ProduceThread p1 = new ProduceThread(godOwn, 50);
        ProduceThread p2 = new ProduceThread(godOwn, 30);
        ProduceThread p3 = new ProduceThread(godOwn, 20);
        ProduceThread p4 = new ProduceThread(godOwn, 20);
        ProduceThread p5 = new ProduceThread(godOwn, 20);
        ResuseThread r1 = new ResuseThread(godOwn, 20);
        ResuseThread r2 = new ResuseThread(godOwn, 20);
        ResuseThread r3 = new ResuseThread(godOwn, 20);
        p1.start();
        p2.start();
        p3.start();
        p4.start();
        p5.start();
        r1.start();
        r2.start();
        r3.start();
    }

 运行结果:

 

生产了50个,仓库共有50个!
消费了20个,仓库共有30个!
生产了20个,仓库共有50个!
消费了20个,仓库共有30个!
消费了20个,仓库共有10个!
生产了20个,仓库共有30个!
生产了20个,仓库共有50个!

 

以上只是一个简单的例子,为了说明Lock Condition的用处!

相关标签: java Lock Condition