E
- data type of queued recordspublic class FastBlockingQueue<E>
extends java.util.AbstractCollection<E>
implements java.util.concurrent.BlockingQueue<E>, java.io.Serializable
Constructor and Description |
---|
FastBlockingQueue(int capacity)
Create a FastBlockingQueue with the specified maximum queue backlog.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e)
Add an element to the queue, throwing an exception if the queue is full and has no
more room.
|
void |
clear()
Remove everything from the queue.
|
boolean |
contains(java.lang.Object o)
Check if this queue contains the specified object (or at least an instance that matches by
equals()).
|
int |
drainTo(java.util.Collection<? super E> c)
Removes all available elements from this queue and adds them
to the given collection.
|
int |
drainTo(java.util.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.
|
int |
drainTo(E[] a)
Removes at most the given number of available elements from
this queue and adds them to the given array.
|
E |
element()
Retrieves, but does not remove, the head of this queue.
|
void |
expandCapacity(int capacity)
Enlarge the queue's backlog capacity.
|
boolean |
fastOffer(E e)
Inserts the specified element into this queue if it is possible to do
so immediately without violating capacity restrictions.
|
int |
fastSize()
Returns the number of elements in this queue, not using synchronization to save time.
|
int |
getCapacity()
Report the total number of queue slots in the queue.
|
java.util.Iterator<E> |
iterator()
Returns an iterator over the elements contained in this collection.
|
boolean |
offer(E e)
Inserts the specified element into this queue if it is possible to do
so immediately without violating capacity restrictions.
|
boolean |
offer(E e,
long timeout,
java.util.concurrent.TimeUnit unit)
Inserts the specified element into this queue, waiting up to the
specified wait time if necessary for space to become available.
|
E |
peek()
Retrieves, but does not remove, the head of this queue,
or returns
null if this queue is empty. |
E |
poll()
Retrieves and removes the head of this queue,
or returns
null if this queue is empty. |
E |
poll(long timeout,
java.util.concurrent.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.
|
void |
putAll(E[] e,
int length)
Inserts all the non-null elements in the specified array into this queue, waiting
if necessary for sufficient 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 |
remove()
Retrieves and removes the head of this queue.
|
void |
setCapacity(int capacity)
Change the queue's backlog capacity.
|
int |
size()
Returns the number of elements in this queue.
|
E |
take()
Retrieves and removes the head of this queue, waiting if necessary
until an element becomes available.
|
addAll, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
public FastBlockingQueue(int capacity)
capacity
- int maximum capacity of queuepublic void clear()
public int drainTo(java.util.Collection<? super E> c)
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.drainTo
in interface java.util.concurrent.BlockingQueue<E>
c
- the collection to transfer elements intojava.lang.UnsupportedOperationException
- if addition of elements
is not supported by the specified collectionjava.lang.ClassCastException
- if the class of an element of this queue
prevents it from being added to the specified collectionjava.lang.NullPointerException
- if the specified collection is nulljava.lang.IllegalArgumentException
- if the specified collection is this
queue, or some property of an element of this queue prevents
it from being added to the specified collectionpublic java.util.Iterator<E> iterator()
public int size()
public final int fastSize()
public void put(E e) throws java.lang.InterruptedException
put
in interface java.util.concurrent.BlockingQueue<E>
e
- the element to addjava.lang.InterruptedException
- if interrupted while waitingjava.lang.ClassCastException
- if the class of the specified element
prevents it from being added to this queuejava.lang.NullPointerException
- if the specified element is nulljava.lang.IllegalArgumentException
- if some property of the specified
element prevents it from being added to this queuepublic void putAll(E[] e, int length) throws java.lang.InterruptedException
e
- the array of elements to addlength
- the int length of the array to test (not all of array may be in use)java.lang.InterruptedException
- if interrupted while waitingjava.lang.ClassCastException
- if the class of the specified element
prevents it from being added to this queuejava.lang.NullPointerException
- if the specified element is nulljava.lang.IllegalArgumentException
- if some property of the specified
element prevents it from being added to this queuepublic boolean offer(E e, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
offer
in interface java.util.concurrent.BlockingQueue<E>
e
- the element to addtimeout
- how long to wait before giving up, in units of
unit
unit
- a TimeUnit
determining how to interpret the
timeout
parametertrue
if successful, or false
if
the specified waiting time elapses before space is availablejava.lang.InterruptedException
- if interrupted while waitingjava.lang.ClassCastException
- if the class of the specified element
prevents it from being added to this queuejava.lang.NullPointerException
- if the specified element is nulljava.lang.IllegalArgumentException
- if some property of the specified
element prevents it from being added to this queuepublic E take() throws java.lang.InterruptedException
take
in interface java.util.concurrent.BlockingQueue<E>
java.lang.InterruptedException
- if interrupted while waitingpublic E poll(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
poll
in interface java.util.concurrent.BlockingQueue<E>
timeout
- how long to wait before giving up, in units of
unit
unit
- a TimeUnit
determining how to interpret the
timeout
parameternull
if the
specified waiting time elapses before an element is availablejava.lang.InterruptedException
- if interrupted while waitingpublic int remainingCapacity()
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.
remainingCapacity
in interface java.util.concurrent.BlockingQueue<E>
public int drainTo(java.util.Collection<? super E> c, int maxElements)
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.drainTo
in interface java.util.concurrent.BlockingQueue<E>
c
- the collection to transfer elements intomaxElements
- the maximum number of elements to transferjava.lang.UnsupportedOperationException
- if addition of elements
is not supported by the specified collectionjava.lang.ClassCastException
- if the class of an element of this queue
prevents it from being added to the specified collectionjava.lang.NullPointerException
- if the specified collection is nulljava.lang.IllegalArgumentException
- if the specified collection is this
queue, or some property of an element of this queue prevents
it from being added to the specified collectionpublic int drainTo(E[] a)
a
may result in elements being in neither,
either or both queue and array when the associated exception is
thrown. Further, the behavior of
this operation is undefined if the specified array is
modified while the operation is in progress.a
- the array to transfer elements intojava.lang.ClassCastException
- if the class of an element of this queue
prevents it from being added to the specified arrayjava.lang.NullPointerException
- if the specified array is nulljava.lang.IllegalArgumentException
- if some property of an element of this queue prevents
it from being added to the specified arraypublic boolean offer(E e)
add(E)
, which can fail to insert an element only
by throwing an exception.offer
in interface java.util.concurrent.BlockingQueue<E>
offer
in interface java.util.Queue<E>
e
- the element to addtrue
if the element was added to this queue, else
false
java.lang.ClassCastException
- if the class of the specified element
prevents it from being added to this queuejava.lang.NullPointerException
- if the specified element is null and
this queue does not permit null elementsjava.lang.IllegalArgumentException
- if some property of this element
prevents it from being added to this queuepublic boolean fastOffer(E e)
add(E)
, which can fail to insert an element only
by throwing an exception.
Note that this method should not be used and the offer(Object)
method should be used instead if
take()
andsize()
or fastSize()
) before calling take()
,e
- the element to addtrue
if the element was added to this queue, else
false
java.lang.ClassCastException
- if the class of the specified element
prevents it from being added to this queuejava.lang.NullPointerException
- if the specified element is null and
this queue does not permit null elementsjava.lang.IllegalArgumentException
- if some property of this element
prevents it from being added to this queuepublic boolean add(E e)
public E poll()
null
if this queue is empty.poll
in interface java.util.Queue<E>
null
if this queue is emptypublic E peek()
null
if this queue is empty.peek
in interface java.util.Queue<E>
null
if this queue is emptypublic E element()
peek
only in that it throws an exception if
this queue is empty.
This implementation returns the result of peek
unless the queue is empty.
element
in interface java.util.Queue<E>
java.util.NoSuchElementException
- if this queue is emptypublic E remove()
poll
only in that it throws an exception if this
queue is empty.
This implementation returns the result of poll
unless the queue is empty.
remove
in interface java.util.Queue<E>
java.util.NoSuchElementException
- if this queue is emptypublic boolean contains(java.lang.Object o)
public int getCapacity()
public void expandCapacity(int capacity)
capacity
- the new backlog capacity for the queuepublic void setCapacity(int capacity) throws java.lang.IllegalArgumentException
capacity
- the new backlog capacity for the queuejava.lang.IllegalArgumentException
- if new capacity is less than the number of
entries still in the queue