net.sourceforge.hiveutils.collections
Interface Queue<T>

All Known Implementing Classes:
QueueImpl

public interface Queue<T>

Minimal interface for a FIFO Queue, hiding implementation details. The queue implementation is supposed to support multi-thread access.

In case of multi-thread read access, please note that the FIFO order cannot be guaranteed any more.

Author:
Jean-Francois Poilpret

Method Summary
 void add(T o)
          Add an item at the top of this Queue.
 boolean isEmpty()
          Indicates if this Queue is empty.
 java.util.List<T> take()
          Take all the items currently in this Queue.
 java.util.List<T> take(int minCount)
          Take all the items currently in this Queue, with a minimum number of items.
 java.util.List<T> take(int minCount, long timeout)
          Take all the items currently in this Queue, with a minimum number of items.
 java.util.List<T> take(long timeout)
          Take all the items currently in this Queue.
 void unblock()
          Unblocks all threads currently waiting on the Take method.
 

Method Detail

isEmpty

boolean isEmpty()
Indicates if this Queue is empty.

Returns:
true if the queue is empty

add

void add(T o)
Add an item at the top of this Queue.

Parameters:
o - the item to add to the queue

take

java.util.List<T> take()
Take all the items currently in this Queue. If the queue is empty, block until one item (at least) is added to it. The returned items are removed from the Queue.

It is possible to unblock this method by calling unblock.

Returns:
the list of all items in the queue, in FIFO order.

take

java.util.List<T> take(int minCount)
Take all the items currently in this Queue, with a minimum number of items. If the queue is empty or does not contain the required amount, block until this amount of items has been added to the Queue. The returned items are removed from the Queue.

Note that if several threads are blocked on this Queue, the first unblocked thread is unpredictable but will generally be the one with the least requirements.

It is possible to unblock this method by calling unblock.

Parameters:
minCount - minimum number of items that must be returned
Returns:
the list of all items in the queue, in FIFO order.

take

java.util.List<T> take(long timeout)
Take all the items currently in this Queue. If the queue is empty, block until one item (at least) is added to it or until the given timeout has ellapsed. The returned items are removed from the Queue.

Parameters:
timeout - maximum number of milliseconds to wait until the queue has an item to be returned by this method
Returns:
the list of all items in the queue, in FIFO order.

take

java.util.List<T> take(int minCount,
                       long timeout)
Take all the items currently in this Queue, with a minimum number of items. If the queue is empty or does not contain the required amount, block until this amount of items has been added to the Queue or until the given timeout has ellapsed. The returned items are removed from the Queue.

Note that if several threads are blocked on this Queue, the first unblocked thread is unpredictable but will generally be the one with the least requirements.

Parameters:
minCount - minimum number of items that must be returned
timeout - maximum number of milliseconds to wait until the queue has an item to be returned by this method
Returns:
the list of all items in the queue, in FIFO order.

unblock

void unblock()
Unblocks all threads currently waiting on the Take method. This can be useful at shutdown time to unblock a thread that would wait indefinately for a minimum number of items: you can unblock this thread so that it will receive all current items, even if this amount is less than the minimum required.