Features Of BlockingQueue in Java
December 09, 20121. 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
It is supported by an array. That is why this is known as ArrayBlockingQueue.
Only those elements are inserted in this queue whose delay has expired.
It is based on link nodes that is why it is known as LinkedBlockingQueue. It supports linked list property.
It provdes the ordering of elements as we get in PriorityQueue and that is why it is known as PriorityBlockingQueue.
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();