Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a capacity-restricted queue implementation?

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?

like image 707
Santhosh Nagulanchi Avatar asked Oct 04 '22 11:10

Santhosh Nagulanchi


2 Answers

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.

like image 141
2 revs Avatar answered Oct 10 '22 02:10

2 revs


ArrayBlockingQueue in NOT the one and only queue in JDK which maintains its internal capacity. The interface java.util.concurrent.BlockingQueue (which is a subinterface of java.util.Queue) has the method remainingCapacity(). All BlockingQueue implementation must implement remainingCapacity().

Some of these implementations do not want to use capacity restrictions. They just return with Integer.MAX_VALUE. They are:

java.util.concurrent.ScheduledThreadPoolExecutor.DelayedWorkQueue.remainingCapacity() java.util.concurrent.DelayQueue.remainingCapacity()
java.util.concurrent.PriorityBlockingQueue.remainingCapacity()
java.util.concurrent.LinkedTransferQueue.remainingCapacity()

There is a special one because it has only 0 capacity:

java.util.concurrent.SynchronousQueue.remainingCapacity()

And the capacity restricting implementations are using an implementation logic like capacity - counter:

java.util.concurrent.ArrayBlockingQueue.remainingCapacity()
java.util.concurrent.LinkedBlockingQueue.remainingCapacity()
java.util.concurrent.LinkedBlockingDeque.remainingCapacity()

Capacity restricting queue implementations are blocking those methods which want to increase the internal count above the internal capacity.

like image 45
Laszlo Hirdi Avatar answered Oct 10 '22 03:10

Laszlo Hirdi