| java.util.concurrent.ArrayBlockingQueue
ArrayBlockingQueue | public class ArrayBlockingQueue extends AbstractQueue implements BlockingQueue<E>,java.io.Serializable(Code) | | A bounded
backed by an
array. This queue orders elements FIFO (first-in-first-out). The
head of the queue is that element that has been on the
queue the longest time. The tail of the queue is that
element that has been on the queue the shortest time. New elements
are inserted at the tail of the queue, and the queue retrieval
operations obtain elements at the head of the queue.
This is a classic "bounded buffer", in which a
fixed-sized array holds elements inserted by producers and
extracted by consumers. Once created, the capacity cannot be
increased. Attempts to offer an element to a full queue will
result in the offer operation blocking; attempts to retrieve an
element from an empty queue will similarly block.
This class supports an optional fairness policy for ordering
waiting producer and consumer threads. By default, this ordering
is not guaranteed. However, a queue constructed with fairness set
to true grants threads access in FIFO order. Fairness
generally decreases throughput but reduces variability and avoids
starvation.
This class implements all of the optional methods
of the
Collection and
Iterator interfaces.
This class is a member of the
Java Collections Framework.
since: 1.5 author: Doug Lea< Parameters: E - > the type of elements held in this collection |
Constructor Summary | |
public | ArrayBlockingQueue(int capacity) Creates an ArrayBlockingQueue with the given (fixed)
capacity and default access policy. | public | ArrayBlockingQueue(int capacity, boolean fair) Creates an ArrayBlockingQueue with the given (fixed)
capacity and the specified access policy. | public | ArrayBlockingQueue(int capacity, boolean fair, Collection<? extends E> c) Creates an ArrayBlockingQueue with the given (fixed)
capacity, the specified access policy and initially containing the
elements of the given collection,
added in traversal order of the collection's iterator. |
Method Summary | |
public void | clear() | public boolean | contains(Object o) | public int | drainTo(Collection<? super E> c) | public int | drainTo(Collection<? super E> c, int maxElements) | final int | inc(int i) Circularly increment i. | public Iterator<E> | iterator() Returns an iterator over the elements in this queue in proper sequence. | public boolean | offer(E o) Inserts the specified element at the tail of this queue if possible,
returning immediately if this queue is full.
Parameters: o - the element to add. | public boolean | offer(E o, long timeout, TimeUnit unit) Inserts the specified element at the tail of this queue, waiting if
necessary up to the specified wait time for space to become available. | public E | peek() | public E | poll() | public E | poll(long timeout, TimeUnit unit) | public void | put(E o) Adds the specified element to the tail of this queue, waiting if
necessary for space to become available. | public int | remainingCapacity() Returns the number of elements that this queue can ideally (in
the absence of memory or resource constraints) accept without
blocking. | public boolean | remove(Object o) | void | removeAt(int i) Utility for remove and iterator.remove: Delete item at position i. | public int | size() Returns the number of elements in this queue. | public E | take() | public Object[] | toArray() | public T[] | toArray(T[] a) | public String | toString() |
ArrayBlockingQueue | public ArrayBlockingQueue(int capacity)(Code) | | Creates an ArrayBlockingQueue with the given (fixed)
capacity and default access policy.
Parameters: capacity - the capacity of this queue throws: IllegalArgumentException - if capacity is less than 1 |
ArrayBlockingQueue | public ArrayBlockingQueue(int capacity, boolean fair)(Code) | | Creates an ArrayBlockingQueue with the given (fixed)
capacity and the specified access policy.
Parameters: capacity - the capacity of this queue Parameters: fair - if true then queue accesses for threads blockedon insertion or removal, are processed in FIFO order; if falsethe access order is unspecified. throws: IllegalArgumentException - if capacity is less than 1 |
ArrayBlockingQueue | public ArrayBlockingQueue(int capacity, boolean fair, Collection<? extends E> c)(Code) | | Creates an ArrayBlockingQueue with the given (fixed)
capacity, the specified access policy and initially containing the
elements of the given collection,
added in traversal order of the collection's iterator.
Parameters: capacity - the capacity of this queue Parameters: fair - if true then queue accesses for threads blockedon insertion or removal, are processed in FIFO order; if falsethe access order is unspecified. Parameters: c - the collection of elements to initially contain throws: IllegalArgumentException - if capacity is less thanc.size(), or less than 1. throws: NullPointerException - if c or any element within itis null |
clear | public void clear()(Code) | | |
inc | final int inc(int i)(Code) | | Circularly increment i.
|
iterator | public Iterator<E> iterator()(Code) | | Returns an iterator over the elements in this queue in proper sequence.
The returned Iterator is a "weakly consistent" iterator that
will never throw
java.util.ConcurrentModificationException ,
and guarantees to traverse elements as they existed upon
construction of the iterator, and may (but is not guaranteed to)
reflect any modifications subsequent to construction.
an iterator over the elements in this queue in proper sequence. |
offer | public boolean offer(E o)(Code) | | Inserts the specified element at the tail of this queue if possible,
returning immediately if this queue is full.
Parameters: o - the element to add. true if it was possible to add the element tothis queue, else false throws: NullPointerException - if the specified element is null |
offer | public boolean offer(E o, long timeout, TimeUnit unit) throws InterruptedException(Code) | | Inserts the specified element at the tail of this queue, waiting if
necessary up to the specified wait time for space to become available.
Parameters: o - the element to add Parameters: timeout - how long to wait before giving up, in units ofunit Parameters: unit - a TimeUnit determining how to interpret thetimeout parameter true if successful, or false ifthe specified waiting time elapses before space is available. throws: InterruptedException - if interrupted while waiting. throws: NullPointerException - if the specified element is null. |
remainingCapacity | public int remainingCapacity()(Code) | | Returns the number of elements that this queue can ideally (in
the absence of memory or resource constraints) accept without
blocking. This is always equal to the initial capacity of this queue
less the current size of this queue.
Note that you cannot always tell if
an attempt to add an element will succeed by
inspecting remainingCapacity because it may be the
case that a waiting consumer is ready to take an
element out of an otherwise full queue.
|
removeAt | void removeAt(int i)(Code) | | Utility for remove and iterator.remove: Delete item at position i.
Call only when holding lock.
|
size | public int size()(Code) | | Returns the number of elements in this queue.
the number of elements in this queue. |
toArray | public T[] toArray(T[] a)(Code) | | |
|
|