activemq-cpp-3.6.0
decaf::util::concurrent::BlockingQueue< E > Class Template Reference

A decaf::util::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. More...

#include <src/main/decaf/util/concurrent/BlockingQueue.h>

Inheritance diagram for decaf::util::concurrent::BlockingQueue< E >:

Public Member Functions

virtual ~BlockingQueue ()
virtual void put (const E &value)=0
 Inserts the specified element into this queue, waiting if necessary for space to become available.
virtual bool offer (const E &e, long long timeout, const TimeUnit &unit)=0
 Inserts the specified element into this queue, waiting up to the specified wait time if necessary for space to become available.
virtual E take ()=0
 Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.
virtual bool poll (E &result, long long timeout, const TimeUnit &unit)=0
 Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.
virtual int remainingCapacity () const =0
 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.
virtual int drainTo (Collection< E > &c)=0
 Removes all available elements from this queue and adds them to the given collection.
virtual int drainTo (Collection< E > &c, int maxElements)=0
 Removes at most the given number of available elements from this queue and adds them to the given collection.
- Public Member Functions inherited from decaf::util::AbstractQueue< E >
 AbstractQueue ()
virtual ~AbstractQueue ()
virtual bool add (const E &value)
 Returns true if this collection changed as a result of the call.(Returns false if this collection does not permit duplicates and already contains the specified element.)Collections that support this operation may place limitations on what elements may be added to this collection. In particular, some collections will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. Collection classes should clearly specify in their documentation any restrictions on what elements may be added.If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false). This preserves the invariant that a collection always contains the specified element after this call returns.For non-pointer values, i.e. class instances or string's the object will be copied into the collection, thus the object must support being copied, must not hide the copy constructor and assignment operator.
Parameters
valueThe reference to the element to add to this Collection.
Returns
true if the element was added to this Collection.
Exceptions
UnsupportedOperationExceptioif this is an unmodifiable collection.
NullPointerExceptionif the Collection is a container of pointers and does not allow NULL values.
IllegalArgumentExceptionif some property of the element prevents it from being added to this collection
IllegalStateExceptionif the element cannot be added at this time due to insertion restrictions.

virtual bool addAll (const Collection< E > &collection)
 Adds all of the elements in the specified collection to this collection.The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.)
Parameters
collectionThe Collection whose elements are added to this one.
Returns
true if this collection changed as a result of the call
Exceptions
UnsupportedOperationExceptioif this is an unmodifiable collection.
NullPointerExceptionif the Collection is a container of pointers and does not allow NULL values.
IllegalArgumentExceptionif some property of an element prevents it from being added to this collection
IllegalStateExceptionif an element cannot be added at this time due to insertion restrictions.
This implementation iterates over the specified collection, and adds each object returned by the iterator to this collection, in turn.Note that this implementation will throw an UnsupportedOperationException unless add is overridden (assuming the specified collection is non-empty).
virtual E remove ()
 Gets and removes the element in the head of the queue.Throws a NoSuchElementException if there is no element in the queue.
Returns
the element in the head of the queue.
Exceptions
NoSuchElementExceptionif there is no element in the queue.

virtual E element () const
 Gets but not removes the element in the head of the queue.Throws a NoSuchElementException if there is no element in the queue.
Returns
the element in the head of the queue.
Exceptions
NoSuchElementExceptionif there is no element in the queue.

virtual void clear ()
 Removes all of the elements from this collection (optional operation).The collection will be empty after this method returns.This implementation iterates over this collection, removing each element using the Iterator.remove operation. Most implementations will probably choose to override this method for efficiency.Note that this implementation will throw an UnsupportedOperationException if the iterator returned by this collection's iterator method does not implement the remove method and this collection is non-empty.
Exceptions
UnsupportedOperationExceptionif the clear operation is not supported by this collection

- Public Member Functions inherited from decaf::util::Queue< E >
virtual ~Queue ()
virtual bool offer (const E &value)=0
 Inserts the specified element into the queue provided that the condition allows such an operation.
virtual bool poll (E &result)=0
 Gets and removes the element in the head of the queue.
virtual bool peek (E &result) const =0
 Gets but not removes the element in the head of the queue.
- Public Member Functions inherited from decaf::util::Collection< E >
virtual ~Collection ()
virtual int size () const =0
 Returns the number of elements in this collection.
- Public Member Functions inherited from decaf::lang::Iterable< E >
virtual ~Iterable ()
virtual decaf::util::Iterator
< E > * 
iterator ()=0
virtual decaf::util::Iterator
< E > * 
iterator () const =0
- Public Member Functions inherited from decaf::util::concurrent::Synchronizable
virtual ~Synchronizable ()
- Public Member Functions inherited from decaf::util::AbstractCollection< E >
 AbstractCollection ()
virtual ~AbstractCollection ()
AbstractCollection< E > & operator= (const AbstractCollection< E > &collection)
 Assignment Operator, copy element from the source collection to this collection after clearing any element stored in this collection.
virtual bool contains (const E &value) const
 Returns true if this collection contains the specified element.More formally, returns true if and only if this collection contains at least one element e such that (value == NULL ? e == NULL : value == e ).
Parameters
valueThe value to check for presence in the collection.
Returns
true if there is at least one of the elements in the collection
Exceptions
NullPointerExceptionif the Collection contains pointers and the Collection does not allow for NULL elements (optional check).

virtual bool containsAll (const Collection< E > &collection) const
 Returns true if this collection contains all of the elements in the specified collection.
Parameters
collectionThe Collection to compare to this one.
Exceptions
NullPointerExceptionif the Collection contains pointers and the Collection does not allow for NULL elements (optional check).

virtual bool equals (const Collection< E > &collection) const
 Answers true if this Collection and the one given are the same size and if each element contained in the Collection given is equal to an element contained in this collection.
virtual void copy (const Collection< E > &collection)
 Renders this Collection as a Copy of the given Collection.
virtual bool isEmpty () const
 Returns true if this collection contains no elements.
virtual bool add (const E &value DECAF_UNUSED)
 
virtual bool remove (const E &value)
 Removes a single instance of the specified element from the collection.More formally, removes an element e such that (value == NULL ? e == NULL : value == e), if this collection contains one or more such elements. Returns true if this collection contained the specified element (or equivalently, if this collection changed as a result of the call).
Parameters
valueThe reference to the element to remove from this Collection.
Returns
true if the collection was changed, false otherwise.
Exceptions
UnsupportedOperationExceptioif this is an unmodifiable collection.
NullPointerExceptionif the Collection is a container of pointers and does not allow NULL values.

virtual bool removeAll (const Collection< E > &collection)
 Removes all this collection's elements that are also contained in the specified collection (optional operation).After this call returns, this collection will contain no elements in common with the specified collection.
Parameters
collectionThe Collection whose elements are to be removed from this one.
Returns
true if the collection changed as a result of this call.
Exceptions
UnsupportedOperationExceptioif this is an unmodifiable collection.
NullPointerExceptionif the Collection is a container of pointers and does not allow NULL values.

virtual bool retainAll (const Collection< E > &collection)
 Retains only the elements in this collection that are contained in the specified collection (optional operation).In other words, removes from this collection all of its elements that are not contained in the specified collection.
Parameters
collectionThe Collection whose elements are to be retained.
Returns
true if the collection changed as a result of this call.
Exceptions
UnsupportedOperationExceptioif this is an unmodifiable collection.
NullPointerExceptionif the Collection is a container of pointers and does not allow NULL values.

virtual std::vector< E > toArray () const
 Answers an STL vector containing copies of all elements contained in this Collection.
virtual void lock ()
 Locks the object.
virtual bool tryLock ()
 Attempts to Lock the object, if the lock is already held by another thread than this method returns false.
virtual void unlock ()
 Unlocks the object.
virtual void wait ()
 Waits on a signal from this object, which is generated by a call to Notify.
virtual void wait (long long millisecs)
 Waits on a signal from this object, which is generated by a call to Notify.
virtual void wait (long long millisecs, int nanos)
 Waits on a signal from this object, which is generated by a call to Notify.
virtual void notify ()
 Signals a waiter on this object that it can now wake up and continue.
virtual void notifyAll ()
 Signals the waiters on this object that it can now wake up and continue.

Additional Inherited Members

- Protected Attributes inherited from decaf::util::AbstractCollection< E >
util::concurrent::Mutex mutex

Detailed Description

template<typename E>
class decaf::util::concurrent::BlockingQueue< E >

A decaf::util::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 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 true 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 Boolean Flag Blocks Times out
Insert add(e) offer(e) put(e) offer(e, time, unit)
Remove remove() poll() take() poll(time, unit)
Examine element() peek() not applicable not applicable

A BlockingQueue may be capacity bounded. At any given time it may have a remainingCapacity beyond which no additional elements can be put without blocking. A BlockingQueue without any intrinsic capacity constraints always reports a remaining capacity of Integer::MAX_VALUE.

BlockingQueue implementations are designed to be used primarily for producer-consumer queues, but additionally support decaf::util::Collection interface. So, for example, it is possible to remove an arbitrary element from a queue using remove(x). However, such operations are in general not performed very efficiently, and are intended for only occasional use, such as when a queued message is cancelled.

BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control. However, the bulk Collection operations addAll, containsAll, retainAll and removeAll are not necessarily performed atomically unless specified otherwise in an implementation. So it is possible, for example, for addAll(c) to fail (throwing an exception) after adding only some of the elements in c.

A BlockingQueue does not intrinsically support any kind of "close" or "shutdown" operation to indicate that no more items will be added. The needs and usage of such features tend to be implementation-dependent. For example, a common tactic is for producers to insert special end-of-stream or poison objects, that are interpreted accordingly when taken by consumers.

Usage example, based on a typical producer-consumer scenario. Note that a BlockingQueue can safely be used with multiple producers and multiple consumers.

class Producer : public Runnable {
private:
    BlockingQueue* queue;
public:
    Producer( BlockingQueue* q ) : queue( q ) {}
    virtual void run() {
        try {
            while( true ) { queue->put( produce() ); }
        } catch( InterruptedException& ex ) { ... handle ...}
    }
    Object produce() { ... }
}
class Consumer : public Runnable {
private:
    BlockingQueue* queue;
public:
    Consumer( BlockingQueue* q ) : queue( q ) {}
    virtual void run() {
        try {
            while( true ) { consume( queue->take() ); }
        } catch( InterruptedException& ex ) { ... handle ...}
    }
    void consume( Object& x ) { ... }
}
int main( int argc, char** argv ) {
    BlockingQueue q = new SomeQueueImplementation();
    Producer p( &q );
    Consumer c1( &q );
    Consumer c2( &q );
    Thread t1( &p ).start();
    Thread t2( &c1 ).start();
    Thread t3( &c2 ).start();
}

Memory consistency effects: As with other concurrent collections, actions in a thread prior to placing an object into a BlockingQueue happen-before actions subsequent to the access or removal of that element from the BlockingQueue in another thread.

Since
1.0

Constructor & Destructor Documentation

template<typename E>
virtual decaf::util::concurrent::BlockingQueue< E >::~BlockingQueue ( )
inlinevirtual

Member Function Documentation

template<typename E>
virtual int decaf::util::concurrent::BlockingQueue< E >::drainTo ( Collection< E > &  c)
pure virtual

Removes all available elements from this queue and adds them to the given collection.

This operation may be more efficient than repeatedly polling this queue. A failure encountered while attempting to add elements to collection c may result in elements being in neither, either or both collections when the associated exception is thrown. Attempts to drain a queue to itself result in IllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

Parameters
cthe collection to transfer elements into
Returns
the number of elements transferred
Exceptions
UnsupportedOperationExceptionif addition of elements is not supported by the specified collection
IllegalArgumentExceptionif the specified collection is this queue, or some property of an element of this queue prevents it from being added to the specified collection

Implemented in decaf::util::concurrent::LinkedBlockingQueue< E >, decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >, and decaf::util::concurrent::SynchronousQueue< E >.

template<typename E>
virtual int decaf::util::concurrent::BlockingQueue< E >::drainTo ( Collection< E > &  c,
int  maxElements 
)
pure virtual

Removes at most the given number of available elements from this queue and adds them to the given collection.

A failure encountered while attempting to add elements to collection c may result in elements being in neither, either or both collections when the associated exception is thrown. Attempts to drain a queue to itself result in IllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

Parameters
cthe collection to transfer elements into
maxElementsthe maximum number of elements to transfer
Returns
the number of elements transferred
Exceptions
UnsupportedOperationExceptionif addition of elements is not supported by the specified collection
IllegalArgumentExceptionif the specified collection is this queue, or some property of an element of this queue prevents it from being added to the specified collection

Implemented in decaf::util::concurrent::LinkedBlockingQueue< E >, decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >, and decaf::util::concurrent::SynchronousQueue< E >.

template<typename E>
virtual bool decaf::util::concurrent::BlockingQueue< E >::offer ( const E &  e,
long long  timeout,
const TimeUnit unit 
)
pure virtual

Inserts the specified element into this queue, waiting up to the specified wait time if necessary for space to become available.

Parameters
ethe element to add
timeouthow long to wait before giving up, in units of unit
unita TimeUnit determining how to interpret the timeout parameter
Returns
true if successful, or false if the specified waiting time elapses before space is available
Exceptions
InterruptedExceptionif interrupted while waiting
NullPointerExceptionif the specified element is null
IllegalArgumentExceptionif some property of the specified element prevents it from being added to this queue

Implemented in decaf::util::concurrent::LinkedBlockingQueue< E >, decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >, and decaf::util::concurrent::SynchronousQueue< E >.

template<typename E>
virtual bool decaf::util::concurrent::BlockingQueue< E >::poll ( E &  result,
long long  timeout,
const TimeUnit unit 
)
pure virtual

Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.

Parameters
resultthe referenced value that will be assigned the value retrieved from the Queue. Undefined if this methods returned false.
timeouthow long to wait before giving up, in units of unit
unita TimeUnit determining how to interpret the timeout parameter.
Returns
true if successful or false if the specified waiting time elapses before an element is available.
Exceptions
InterruptedExceptionif interrupted while waiting

Implemented in decaf::util::concurrent::LinkedBlockingQueue< E >, decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >, and decaf::util::concurrent::SynchronousQueue< E >.

template<typename E>
virtual void decaf::util::concurrent::BlockingQueue< E >::put ( const E &  value)
pure virtual

Inserts the specified element into this queue, waiting if necessary for space to become available.

Parameters
valuethe element to add
Exceptions
InterruptedExceptionif interrupted while waiting
NullPointerExceptionif the specified element is null
IllegalArgumentExceptionif some property of the specified element prevents it from being added to this queue

Implemented in decaf::util::concurrent::LinkedBlockingQueue< E >, decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >, and decaf::util::concurrent::SynchronousQueue< E >.

template<typename E>
virtual int decaf::util::concurrent::BlockingQueue< E >::remainingCapacity ( ) const
pure virtual

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.

Note that you cannot always tell if an attempt to insert an element will succeed by inspecting remainingCapacity because it may be the case that another thread is about to insert or remove an element.

Returns
the remaining capacity

Implemented in decaf::util::concurrent::LinkedBlockingQueue< E >, decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >, and decaf::util::concurrent::SynchronousQueue< E >.

template<typename E>
virtual E decaf::util::concurrent::BlockingQueue< E >::take ( )
pure virtual

Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.

Returns
the head of this queue
Exceptions
InterruptedExceptionif interrupted while waiting

Implemented in decaf::util::concurrent::LinkedBlockingQueue< E >, decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >, and decaf::util::concurrent::SynchronousQueue< E >.


The documentation for this class was generated from the following file: