博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个简单的生产者消费者
阅读量:5740 次
发布时间:2019-06-18

本文共 3430 字,大约阅读时间需要 11 分钟。

使用ReentrantLock以及Condition实现的生产者消费者,也是java 1.5之后推荐写法,不再使用object类的notify()以及wait()方法。直接上代码:

public class Stroge {    private final int MAX_SIZE = 100;    private LinkedList list = new LinkedList<>();    private Lock lock = new ReentrantLock();    private Condition notFull = lock.newCondition();    private Condition notEmpty = lock.newCondition();    public void produce(int num) {        lock.lock();        try {            while (list.size() + num > MAX_SIZE) {                System.out.println("当前size:" + list.size() + ",不能再创建" + num + "个产品,await");                try {                    notFull.await();                } catch (InterruptedException e) {                    e.printStackTrace();                }            }            for (int i = 0; i < num; i++) {                list.add(new Object());            }            System.out.println(num + "个产品创建完成,当前size:" + list.size());            notEmpty.signalAll();        } finally {            lock.unlock();        }    }    public void consume(int num) {        lock.lock();        try {            while (list.size() < num) {                System.out.println("当前size:" + list.size() + ",不能再消费" + num + "个产品,await");                try {                    notEmpty.await();                } catch (InterruptedException e) {                    e.printStackTrace();                }            }            for (int i = 0; i < num; i++) {                list.remove();             }            System.out.println(num + "个产品消费完成,当前size:" + list.size());            notFull.signalAll();        } finally {            lock.unlock();        }    }}//生产者类class Producter extends Thread {    Stroge sto = null;    private int num;    public Producter(Stroge sto) {        this.sto = sto;    }    public void setNum(int num) {        this.num = num;    }    public void product(int num) {        sto.produce(num);    }    @Override    public void run() {        product(num);    }}//消费者类class Consumer extends Thread {    Stroge sto = null;    private int num;    public Consumer(Stroge sto) {        this.sto = sto;    }    public void setNum(int num) {        this.num = num;    }    public void consume(int num) {        sto.consume(num);    }    @Override    public void run() {        consume(num);    }}

测试类:

public class Testprosum {    public static void main(String[] args) {        Stroge sto = new Stroge();        Producter p1 = new Producter(sto);        Producter p2 = new Producter(sto);        Producter p3 = new Producter(sto);        Producter p4 = new Producter(sto);        Producter p5 = new Producter(sto);        Consumer c1 = new Consumer(sto);        Consumer c2 = new Consumer(sto);        Consumer c3 = new Consumer(sto);        Consumer c4 = new Consumer(sto);        Consumer c5 = new Consumer(sto);        p1.setNum(10);        p2.setNum(20);        p3.setNum(30);        p4.setNum(40);        p5.setNum(50);        c1.setNum(10);        c2.setNum(20);        c3.setNum(30);        c4.setNum(40);        c5.setNum(50);        p1.start();        p2.start();        p3.start();        p4.start();        p5.start();        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        c1.start();        c2.start();        c3.start();        c4.start();        c5.start();    }}

ReentrantLock需要注意try{}finally{lock.unlock();}

OVER

转载地址:http://qpbzx.baihongyu.com/

你可能感兴趣的文章
I.MX6 简单电路模拟USB设备的插入
查看>>
(2010-8-31) awk内存泄漏以及缓慢的正则表达式计算速度
查看>>
基于PHPUnit编写测试用例
查看>>
iOS UINavigationBar-导航栏、UINavigationItem-导航项
查看>>
51nod 1284:2 3 5 7的倍数 容斥原理
查看>>
设计mooon调度器遇到的难题
查看>>
学JAVA争做IT精英
查看>>
ipad应用打包
查看>>
Apache和Nignx基于三种方式搭建web站点并设置用户访问控制达到优化整个站点性能...
查看>>
Android 手机应用开发经验之手势识别Gesture的
查看>>
Longest Palindromic Substring
查看>>
ibooks 3d 翻转效果 flip 3d
查看>>
URAL 1992 CVS 可持久化链栈
查看>>
eclipse 列编辑
查看>>
文件方式实现完整的英文词频统计实例
查看>>
Oracle活动会话历史(ASH)及报告解读
查看>>
Project Euler Problem 7: 10001st prime
查看>>
HDU1113 POJ1318 UVA642 ZOJ1181 UVALive5328 Word Amalgamation【MAP+排序】
查看>>
python制作验证码
查看>>
Java Thread(一) Outline
查看>>