Share via


IBlockingQueue Interface

Definition

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.

[Android.Runtime.Register("java/util/concurrent/BlockingQueue", "", "Java.Util.Concurrent.IBlockingQueueInvoker")]
[Java.Interop.JavaTypeParameters(new System.String[] { "E" })]
public interface IBlockingQueue : IDisposable, Java.Interop.IJavaPeerable, Java.Util.IQueue
[<Android.Runtime.Register("java/util/concurrent/BlockingQueue", "", "Java.Util.Concurrent.IBlockingQueueInvoker")>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "E" })>]
type IBlockingQueue = interface
    interface IQueue
    interface ICollection
    interface IIterable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
Derived
Attributes
Implements

Remarks

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 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:

<table class="plain"> <caption>Summary of BlockingQueue methods</caption> <tr> <td></td> <th scope="col" style="font-weight:normal; font-style:italic">Throws exception</th> <th scope="col" style="font-weight:normal; font-style:italic">Special value</th> <th scope="col" style="font-weight:normal; font-style:italic">Blocks</th> <th scope="col" style="font-weight:normal; font-style:italic">Times out</th> </tr> <tr> <th scope="row" style="text-align:left">Insert</th> <td>#add(Object) add(e)</td> <td>#offer(Object) offer(e)</td> <td>#put(Object) put(e)</td> <td>#offer(Object, long, TimeUnit) offer(e, time, unit)</td> </tr> <tr> <th scope="row" style="text-align:left">Remove</th> <td>#remove() remove()</td> <td>#poll() poll()</td> <td>#take() take()</td> <td>#poll(long, TimeUnit) poll(time, unit)</td> </tr> <tr> <th scope="row" style="text-align:left">Examine</th> <td>#element() element()</td> <td>#peek() peek()</td> <td style="font-style: italic">not applicable</td> <td style="font-style: italic">not applicable</td> </tr> </table>

A BlockingQueue does not accept null elements. Implementations throw NullPointerException on attempts to add, put or offer a null. A null is used as a sentinel value to indicate failure of poll operations.

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 the 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 <em>not</em> 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 <em>bulk</em> Collection operations addAll, containsAll, retainAll and removeAll are <em>not</em> 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 <em>not</em> intrinsically support any kind of &quot;close&quot; or &quot;shutdown&quot; 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 <em>end-of-stream</em> or <em>poison</em> 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.

{@code
            class Producer implements Runnable {
              private final BlockingQueue queue;
              Producer(BlockingQueue q) { queue = q; }
              public void run() {
                try {
                  while (true) { queue.put(produce()); }
                } catch (InterruptedException ex) { ... handle ...}
              }
              Object produce() { ... }
            }

            class Consumer implements Runnable {
              private final BlockingQueue queue;
              Consumer(BlockingQueue q) { queue = q; }
              public void run() {
                try {
                  while (true) { consume(queue.take()); }
                } catch (InterruptedException ex) { ... handle ...}
              }
              void consume(Object x) { ... }
            }

            class Setup {
              void main() {
                BlockingQueue q = new SomeQueueImplementation();
                Producer p = new Producer(q);
                Consumer c1 = new Consumer(q);
                Consumer c2 = new Consumer(q);
                new Thread(p).start();
                new Thread(c1).start();
                new Thread(c2).start();
              }
            }}

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

This interface is a member of the Java Collections Framework.

Added in 1.5.

Java documentation for java.util.concurrent.BlockingQueue.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Properties

Handle

Gets the JNI value of the underlying Android object.

(Inherited from IJavaObject)
IsEmpty

Returns if this Collection contains no elements.

(Inherited from ICollection)
JniIdentityHashCode

Returns the value of java.lang.System.identityHashCode() for the wrapped instance.

(Inherited from IJavaPeerable)
JniManagedPeerState

State of the managed peer.

(Inherited from IJavaPeerable)
JniPeerMembers

Member access and invocation support.

(Inherited from IJavaPeerable)
PeerReference

Returns a JniObjectReference of the wrapped Java object instance.

(Inherited from IJavaPeerable)

Methods

Add(Object)

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

AddAll(ICollection)

Adds all of the elements in the specified collection to this collection (optional operation).

(Inherited from ICollection)
Clear()

Removes all of the elements from this collection (optional operation).

(Inherited from ICollection)
Contains(Object)

Returns true if this queue contains the specified element.

ContainsAll(ICollection)

Returns true if this collection contains all of the elements in the specified collection.

(Inherited from ICollection)
Disposed()

Called when the instance has been disposed.

(Inherited from IJavaPeerable)
DisposeUnlessReferenced()

If there are no outstanding references to this instance, then calls Dispose(); otherwise, does nothing.

(Inherited from IJavaPeerable)
DrainTo(ICollection, Int32)

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

DrainTo(ICollection)

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

Element()

Retrieves, but does not remove, the head of this queue.

(Inherited from IQueue)
Equals(Object)

Compares the specified object with this collection for equality.

(Inherited from ICollection)
Finalized()

Called when the instance has been finalized.

(Inherited from IJavaPeerable)
ForEach(IConsumer)

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

(Inherited from IIterable)
GetHashCode()

Returns the hash code value for this collection.

(Inherited from ICollection)
Iterator()

Returns an iterator over the elements in this collection.

(Inherited from ICollection)
Offer(Object, Int64, TimeUnit)

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

Offer(Object)

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.

Peek()

Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

(Inherited from IQueue)
Poll()

Retrieves and removes the head of this queue, or returns null if this queue is empty.

(Inherited from IQueue)
Poll(Int64, TimeUnit)

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

Put(Object)

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

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.

Remove()

Retrieves and removes the head of this queue.

(Inherited from IQueue)
Remove(Object)

Removes a single instance of the specified element from this queue, if it is present.

RemoveAll(ICollection)

Removes all of this collection's elements that are also contained in the specified collection (optional operation).

(Inherited from ICollection)
RemoveIf(IPredicate)

Removes all of the elements of this collection that satisfy the given predicate.

(Inherited from ICollection)
RetainAll(ICollection)

Retains only the elements in this collection that are contained in the specified collection (optional operation).

(Inherited from ICollection)
SetJniIdentityHashCode(Int32)

Set the value returned by JniIdentityHashCode.

(Inherited from IJavaPeerable)
SetJniManagedPeerState(JniManagedPeerStates) (Inherited from IJavaPeerable)
SetPeerReference(JniObjectReference)

Set the value returned by PeerReference.

(Inherited from IJavaPeerable)
Size()

Returns the number of elements in this collection.

(Inherited from ICollection)
Spliterator()

Creates a Spliterator over the elements described by this Iterable.

(Inherited from IIterable)
Take()

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

ToArray()

Returns an array containing all of the elements in this collection.

(Inherited from ICollection)
ToArray(IIntFunction)

Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array.

(Inherited from ICollection)
ToArray(Object[])

Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.

(Inherited from ICollection)
UnregisterFromRuntime()

Unregister this instance so that the runtime will not return it from future Java.Interop.JniRuntime+JniValueManager.PeekValue invocations.

(Inherited from IJavaPeerable)

Explicit Interface Implementations

IIterable.Spliterator()

Creates a Spliterator over the elements in this collection.

(Inherited from ICollection)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)
OfferAsync(IBlockingQueue, Object, Int64, TimeUnit)
OfferAsync(IBlockingQueue, Object)
PollAsync(IBlockingQueue, Int64, TimeUnit)
PutAsync(IBlockingQueue, Object)
TakeAsync(IBlockingQueue)
ToEnumerable(IIterable)
ToEnumerable<T>(IIterable)

Applies to