Advanced   Java   Services BlockingQueues


BlockingQueue<E>

Das Interface BlockingQueue ist eine Erweiterungen des Interfaces Queue und ist speziell für Producer-Consumer Warteschlangen entworfen worden. Da sie einen generischen Typ zulassen, können sie Warteschlangen für beliebige Objekte darstellen. Aus der API entnehmen wir die folgenden Passagen:

"A Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element."

BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control.



Die neuen Methoden von BlockingQueue

Aufgeführt sind nur die wirklich neuen Methoden, nicht diejenigen die geerbt und überschrieben worden sind wie etwa add():

Modifier and TypeMethod and Description
int drainTo(Collection<? super E> c)
Removes all available elements from this queue and adds them to the given collection.
int drainTo(Collection<? super E> c, int maxElements)
Removes at most the given number of available elements from this queue and adds them to the given collection.
boolean offer(E e, long timeout, TimeUnit unit)
Inserts the specified element into this queue, waiting up to the specified wait time if necessary for space to become available.
E poll(long timeout, TimeUnit unit)
Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.
void put(E e)
Inserts the specified element into this queue, waiting if necessary for space to become available.
int remainingCapacity()
Returns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking, or Integer.MAX_VALUE if there is no intrinsic limit.
E take()
Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.


Das Verhalten spezieller Methoden

BlockingQueue methods come in four forms, with different ways of handling operations that cannot be satisfied immediately, but may be satisfied at some point in the future: one throws an exception, the second returns a special value (either null or false, depending on the operation), the third blocks the current thread indefinitely until the operation can succeed, and the fourth blocks for only a given maximum time limit before giving up. These methods are summarized in the following table:

Throws exception Returns special value Times out (neu in BlockingQueues) Blocks (neu in BlockingQueues)
Insert add(E e) offer(E e)   (true or false) offer(E e, long timeout, TimeUnit unit) put(E e)   (blockiert, wenn Queue full)
Remove remove() poll()         (null falls Queue leer) poll(long timeout, TimeUnit unit) take()      (blockiert, wenn Queue leer)
Examine (no remove) element() peek()       (null falls Queue leer) not applicable not applicable


Übersicht über blocking Queues
Bounded FIFO LIFO Head = Longest time element Head = Least element Insert at tail Retrieve from head Permits null Thread safe
ArrayBlockingQueue xx xxxxx
LinkedBloquingQueue xx xxxx
PriorityBlockingQueue x
DelayQueue x
SynchronousQueue x
LinkedTransferQueue x