A blocking queue is a queue that blocks when you try to dequeue from it and the queue is empty, or if you try to enqueue items to it and the queue is already full. A thread trying to dequeue from an empty queue is blocked until some other thread inserts an item into the queue. A thread trying to enqueue an item in a full queue is blocked until some other thread makes space in the queue, either by dequeuing one or more items or clearing the queue completely.
Here is a diagram showing two threads cooperating via a blocking queue:
A BlockingQueue with one thread putting into it, and another thread taking from it. |
Java 5 comes with blocking queue implementations in the
java.util.concurrent
package. You can read about that class in my java.util.concurrent.BlockingQueue tutorial. Even if Java 5 comes with a blocking queue implementation, it can be useful to know the theory behind their implementation.Blocking Queue Implementation
The implementation of a blocking queue looks similar to a Bounded Semaphore. Here is a simple implementation of a blocking queue:
public class BlockingQueue { private List queue = new LinkedList(); private int limit = 10; public BlockingQueue(int limit){ this.limit = limit; } public synchronized void enqueue(Object item) throws InterruptedException { while(this.queue.size() == this.limit) { wait(); } if(this.queue.size() == 0) { notifyAll(); } this.queue.add(item); } public synchronized Object dequeue() throws InterruptedException{ while(this.queue.size() == 0){ wait(); } if(this.queue.size() == this.limit){ notifyAll(); } return this.queue.remove(0); } }
Notice how notifyAll()
is only called from enqueue()
and dequeue()
if the queue size is equal to the size bounds (0 or limit). If the queue size is not equal to either bound when enqueue()
or dequeue()
is called, there can be no threads waiting to either enqueue or dequeue items.
相关推荐
java阻塞队列(Blocking Queue)是一个支持两个附加操作的队列.txt
本篇文章小编为大家介绍,基于java中BlockingQueue的使用介绍。需要的朋友参考下
myeclipse2016的汉语包和破解码,将压缩包中的文件全部替换到安装目录下
类似java BlockingQueue,C++写的,支持Windows与Linux。
Java Concurrency Patterns and Features ...Blocking Queue Executors Fixed Thread Pool Cached Thread Pool Single Thread Pool Scheduled Thread Pool Atomics Futures FutureTask CompletableFuture Java M
BlockingFQueue基于磁盘持久存储的阻塞队列
阻塞队列 COEN 283 的阻塞队列项目 通过阻塞队列传递消息 使用同步的消息缓冲区使用队列来实现阻塞队列,并在队列为空/满时进行自旋等待。 无锁消息缓冲区创建环形缓冲区; 在自旋等待期间使用 LockSupport,并使用...
Concurrent包:Blocking:Queue\Concurrent Hash Map \ExcutorService\lock\原子性 BlockingQueue:ArrayBlockingQueue , DelayQueue , LinkedBlockingDeque , LinkedBlockingQueue , LinkedTransferQueue , ...
线程池管理器用Java实现的简单线程池管理器
主要成分[Non Blocking I/O] => [Queue] => [Data Store]可插入“非阻塞I / O”,“队列”和“数据存储”块。默认值默认情况下,Highlander在“ ” =>“ ” =>“ ”上运行尽管可以使用某些内置组件代替默认配置: ...
Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python ...
Autoattach now happens using a thread instead of a gui blocking timer Some colorscheme enhancements Added a DBVM based "Find what writes/accesses" feature. (For pro users, enable kernelmode options ...