当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

生产者消费者 Lock Condition实现

发表于: 2014-04-02   作者:abc08010051   来源:转载   浏览:
摘要:     网上有很多生产者消费者模型,很多都是基于synchronized, wait, notify的,也有基于Lock的,下面是我用Lock, Condition实现的.   1 仓库:   class GodOwn { //声明锁对象 private Lock lock = new ReentrantLock();

    网上有很多生产者消费者模型,很多都是基于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的用处!

生产者消费者 Lock Condition实现

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号