How to create a capacity-restricted queue implementation?
concurrency, java
Solution
You should use a `BlockingQueue` such as `ArrayBlockingQueue`, which is:
A bounded blocking queue backed by an array. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue.
This is a classic "bounded buffer", in which a fixed-sized array holds elements inserted by producers and extracted by consumers. Once created, the capacity cannot be changed. Attempts to put an element into a full queue will result in the operation blocking; attempts to take an element from an empty queue will similarly block.
Problem
In the Java API documentation, I tried to understand the following explanation from an implementation point of view. http://docs.oracle.com/javase/6/docs/api/java/util/Queue.html Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations. Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation). The latter form of the insert operation is designed specifically for use with capacity-restricted Queue implementations; in most implementations, insert operations cannot fail. So, I would like to write a program to verify, in which scenario it throws an exception. How can I create a capacity-restricted queue implementation and verify? Can someone advice with an example?