Java Reference
In-Depth Information
Blocking Queues
You have seen the behavior of a Queue in two extreme cases:
When you want to add an element to it when it is full
When you want to remove an element from it when it is empty
A queue specifies two types of methods to deal with insertion, removal, and peeking in these two extreme cases:
one type of method throws an exception whereas the other type of method returns a special value.
A blocking queue extends the behavior of a queue in dealing with these extreme cases. It adds two more sets of
methods: one set of methods blocks indefinitely and another set of methods lets you specify a time period to block.
An instance of the BlockingQueue interface represents a blocking queue. The BlockingQueue interface inherits
from the Queue interface. Here are two additional features that the BlockingQueue interface offers:
put() and offer() to let you add an element to the blocking
queue at its tail. The put() method blocks indefinitely if the blocking queue is full until space
becomes available in the queue. The offer() method lets you specify the time period to wait
for space to become available in the blocking queue. It returns true if the specified element
was added successfully; it returns false if the specified time period elapsed before the space
became available for the new element.
It adds two methods called
take() and poll() to let you retrieve and remove the head from
the blocking queue. The take() method blocks indefinitely if the blocking queue is empty.
The poll() method lets you specify a time period to wait if the blocking queue is empty; it
returns null if the specified time elapses before an element became available.
If you use methods from the Queue interface with a BlockingQueue , they would behave as if you are using a Queue .
A BlockingQueue is designed to be thread-safe. Usually it is used in a producer/consumer-like situation where some
threads (called producers) add elements to it and some threads (called consumers) remove elements from it.
A blocking queue does not allow a null element. A blocking queue can be bounded or unbounded. It adds
another method called remainingCapacity() that returns the number of elements that can be added to the blocking
queue without blocking. You need to be careful in basing your decision on the return value of this method. There may
be other threads attempting to add elements to the blocking queue at the same time you call this method. In such
cases, when you attempt to add new elements based on the return value of this method, your elements may not be
added, even though you know that there is some space available. The real test whether an element can be added to a
blocking queue or not is to attempt to add one and check the return value of the put() or offer() method.
There is one more thing that is related to a blocking queue: fairness . Fairness is used to handle situations where
multiple threads are blocked to perform insertion or removal. If a blocking queue is fair, it will allow the longest
waiting thread to perform the operation when a condition arises that allows the operation to proceed. If the blocking
queue is not fair, the order in which the blocked threads are allowed to perform the operation is not specified. Specific
implementations determine fairness availability.
The BlockingQueue interface and all its implementation classes are in the java.util.concurrent package. The
following are the implementation classes for the BlockingQueue interface:
It adds two methods called
ArrayBlockingQueue : It is a bounded implementation class for BlockingQueue . It is backed by
an array. It also lets you specify the fairness of the blocking queue in its constructor. By default,
it is not fair.
LinkedBlockingQueue : It is another implementation class for BlockingQueue . It can be used
as a bounded or unbounded blocking queue. It does not allow specifying a fairness rule for the
blocking queue.
PriorityBlockingQueue : It is an unbounded implementation class for BlockingQueue . It
works the same way as PriortyQueue for ordering the elements in the blocking queue. It adds
the blocking feature to PriorityQueue .
 
Search WWH ::




Custom Search