博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ArrayBlockingQueue源码分析
阅读量:4229 次
发布时间:2019-05-26

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

ArrayBlockingQueue最核心的实现就是一把锁,两个条件。具体看源码如下:

/** Main lock guarding all access */    final ReentrantLock lock;    /** Condition for waiting takes */    private final Condition notEmpty;    /** Condition for waiting puts */    private final Condition notFull;

put和take操作的时候都需要加锁,如果队列满了notFull.wait(),等待消费者消费掉信息后调用notFull.notify(),这样就可以继续put了。如果队列为空的原理也相似。

下面看下put和take的源码

public void put(E e) throws InterruptedException {        checkNotNull(e);        final ReentrantLock lock = this.lock;        lock.lockInterruptibly();        try {            //如果当前数据到达最大值,生产者等待            while (count == items.length)                notFull.await();            enqueue(e);        } finally {            lock.unlock();        }    }            public E take() throws InterruptedException {        final ReentrantLock lock = this.lock;        lock.lockInterruptibly();        try {            //如果当前数据为空,则消费者等待            while (count == 0)                notEmpty.await();            return dequeue();        } finally {            lock.unlock();        }    }

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

你可能感兴趣的文章
Java Database Programming Bible
查看>>
Model Driven Architecture: Applying MDA to Enterprise Computing
查看>>
Pro Jakarta Commons
查看>>
Pro JSP, Third Edition
查看>>
LightWave 3D 8 Revealed
查看>>
The Cg Tutorial: The Definitive Guide to Programmable Real-Time Graphics
查看>>
Operating Systems Design and Implementation (3rd Edition)
查看>>
Beginning Visual C# 2005
查看>>
Professional C# 2005
查看>>
Faster Smarter Beginning Programming
查看>>
The Essence of Object-Oriented Programming with Java and UML
查看>>
ROI of Software Process Improvement: Metrics for Project Managers and Software Engineers
查看>>
FileMaker 8 Functions and Scripts Desk Reference
查看>>
Web Security Field Guide
查看>>
Computer Animation: Algorithms and Techniques
查看>>
Multimedia-based Instructional Design
查看>>
3D Videocommunication : Algorithms, concepts and real-time systems in human centred communication
查看>>
Guide to Computer Animation
查看>>
Character Development and Storytelling for Games
查看>>
Six Sigma--The First 90 Days
查看>>