Home  >  Core Java

Features Of BlockingQueue in Java

By Arvind Rai, December 09, 2012
1. The interesting feature of blocking queue is when we add element and if there is no space to add, BlockingQueue waits for space. In the same way if we retrieve elements from BlockingQueue then it waits to become non-empty.
2. We cannot add null in BlockingQueue.
3. BlockingQueue is thread-safe.
4. Usually BlockingQueue is used for producer consumer implementations.
5. java.util.concurrent.BlockingQueue is an interface.
6. BlockingQueue has been implemented by
ArrayBlockingQueue
It is supported by an array. That is why this is known as ArrayBlockingQueue.
DelayQueue
Only those elements are inserted in this queue whose delay has expired.
LinkedBlockingQueue
It is based on link nodes that is why it is known as LinkedBlockingQueue. It supports linked list property.
PriorityBlockingQueue
It provdes the ordering of elements as we get in PriorityQueue and that is why it is known as PriorityBlockingQueue.
SynchronousQueue
In SynchronousQueue, each put task waits for take and each take task waits for put
7. BlockingQueue does not indicate that no more elements can be added.
8. BlockingQueue provides method like add, take, offer, poll etc.

Example of BlockingQueue

BlockingQueue queue = new ArrayBlockingQueue(1024);

BlockingQueue queue = new DelayQueue();

BlockingQueue queue = new LinkedBlockingQueue();


BlockingQueue queue = new SynchronousQueue();
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us