Class VariableLinkedBlockingQueue<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractQueue<E>
-
- com.rabbitmq.client.impl.VariableLinkedBlockingQueue<E>
-
- Type Parameters:
E- the type of elements held in this collection
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable<E>,java.util.Collection<E>,java.util.concurrent.BlockingQueue<E>,java.util.Queue<E>
public class VariableLinkedBlockingQueue<E> extends java.util.AbstractQueue<E> implements java.util.concurrent.BlockingQueue<E>, java.io.SerializableA clone of LinkedBlockingQueue with the addition of asetCapacity(int)method, allowing us to change the capacity of the queue while it is in use.The documentation for LinkedBlockingQueue follows...
An optionally-bounded blocking queue based on linked nodes. 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. Linked queues typically have higher throughput than array-based queues but less predictable performance in most concurrent applications.
The optional capacity bound constructor argument serves as a way to prevent excessive queue expansion. The capacity, if unspecified, is equal to
Integer.MAX_VALUE. Linked nodes are dynamically created upon each insertion unless this would bring the queue above capacity.This class implements all of the optional methods of the
CollectionandIteratorinterfaces.This class is a member of the Java Collections Framework.
- Since:
- 1.5
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classVariableLinkedBlockingQueue.Itr(package private) static classVariableLinkedBlockingQueue.Node<E>Linked list node class
-
Field Summary
Fields Modifier and Type Field Description private intcapacityThe capacity bound, or Integer.MAX_VALUE if noneprivate java.util.concurrent.atomic.AtomicIntegercountCurrent number of elementsprivate VariableLinkedBlockingQueue.Node<E>headHead of linked listprivate VariableLinkedBlockingQueue.Node<E>lastTail of linked listprivate java.util.concurrent.locks.ConditionnotEmptyWait queue for waiting takesprivate java.util.concurrent.locks.ConditionnotFullWait queue for waiting putsprivate java.util.concurrent.locks.ReentrantLockputLockLock held by put, offer, etcprivate static longserialVersionUIDprivate java.util.concurrent.locks.ReentrantLocktakeLockLock held by take, poll, etc
-
Constructor Summary
Constructors Constructor Description VariableLinkedBlockingQueue()Creates a LinkedBlockingQueue with a capacity ofInteger.MAX_VALUE.VariableLinkedBlockingQueue(int capacity)Creates a LinkedBlockingQueue with the given (fixed) capacity.VariableLinkedBlockingQueue(java.util.Collection<? extends E> c)Creates a LinkedBlockingQueue with a capacity ofInteger.MAX_VALUE, initially containing the elements of the given collection, added in traversal order of the collection's iterator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()intdrainTo(java.util.Collection<? super E> c)intdrainTo(java.util.Collection<? super E> c, int maxElements)private Eextract()Remove a node from head of queue,private voidfullyLock()Lock to prevent both puts and takes.private voidfullyUnlock()Unlock to allow both puts and takes.private voidinsert(E x)Create a node and link it at end of queuejava.util.Iterator<E>iterator()Returns an iterator over the elements in this queue in proper sequence.booleanoffer(E o)Inserts the specified element at the tail of this queue if possible, returning immediately if this queue is full.booleanoffer(E o, long timeout, java.util.concurrent.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.Epeek()Epoll()Epoll(long timeout, java.util.concurrent.TimeUnit unit)voidput(E o)Adds the specified element to the tail of this queue, waiting if necessary for space to become available.private voidreadObject(java.io.ObjectInputStream s)Reconstitute this queue instance from a stream (that is, deserialize it).intremainingCapacity()Returns the number of elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking.booleanremove(java.lang.Object o)voidsetCapacity(int capacity)Set a new capacity for the queue.private voidsignalNotEmpty()Signal a waiting take.private voidsignalNotFull()Signal a waiting put.intsize()Returns the number of elements in this queue.Etake()java.lang.Object[]toArray()<T> T[]toArray(T[] a)java.lang.StringtoString()private voidwriteObject(java.io.ObjectOutputStream s)Save the state to a stream (that is, serialize it).-
Methods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, removeAll, retainAll
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
capacity
private int capacity
The capacity bound, or Integer.MAX_VALUE if none
-
count
private final java.util.concurrent.atomic.AtomicInteger count
Current number of elements
-
head
private transient VariableLinkedBlockingQueue.Node<E> head
Head of linked list
-
last
private transient VariableLinkedBlockingQueue.Node<E> last
Tail of linked list
-
takeLock
private final java.util.concurrent.locks.ReentrantLock takeLock
Lock held by take, poll, etc
-
notEmpty
private final java.util.concurrent.locks.Condition notEmpty
Wait queue for waiting takes
-
putLock
private final java.util.concurrent.locks.ReentrantLock putLock
Lock held by put, offer, etc
-
notFull
private final java.util.concurrent.locks.Condition notFull
Wait queue for waiting puts
-
-
Constructor Detail
-
VariableLinkedBlockingQueue
public VariableLinkedBlockingQueue()
Creates a LinkedBlockingQueue with a capacity ofInteger.MAX_VALUE.
-
VariableLinkedBlockingQueue
public VariableLinkedBlockingQueue(int capacity)
Creates a LinkedBlockingQueue with the given (fixed) capacity.- Parameters:
capacity- the capacity of this queue.- Throws:
java.lang.IllegalArgumentException- if capacity is not greater than zero.
-
VariableLinkedBlockingQueue
public VariableLinkedBlockingQueue(java.util.Collection<? extends E> c)
Creates a LinkedBlockingQueue with a capacity ofInteger.MAX_VALUE, initially containing the elements of the given collection, added in traversal order of the collection's iterator.- Parameters:
c- the collection of elements to initially contain- Throws:
java.lang.NullPointerException- if c or any element within it is null
-
-
Method Detail
-
signalNotEmpty
private void signalNotEmpty()
Signal a waiting take. Called only from put/offer (which do not otherwise ordinarily lock takeLock.)
-
signalNotFull
private void signalNotFull()
Signal a waiting put. Called only from take/poll.
-
insert
private void insert(E x)
Create a node and link it at end of queue- Parameters:
x- the item
-
extract
private E extract()
Remove a node from head of queue,- Returns:
- the node
-
fullyLock
private void fullyLock()
Lock to prevent both puts and takes.
-
fullyUnlock
private void fullyUnlock()
Unlock to allow both puts and takes.
-
size
public int size()
Returns the number of elements in this queue.
-
setCapacity
public void setCapacity(int capacity)
Set a new capacity for the queue. Increasing the capacity can cause any waitingput(Object)invocations to succeed if the new capacity is larger than the queue.- Parameters:
capacity- the new capacity for the queue
-
remainingCapacity
public int remainingCapacity()
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.
- Specified by:
remainingCapacityin interfacejava.util.concurrent.BlockingQueue<E>
-
put
public void put(E o) throws java.lang.InterruptedException
Adds the specified element to the tail of this queue, waiting if necessary for space to become available.- Specified by:
putin interfacejava.util.concurrent.BlockingQueue<E>- Parameters:
o- the element to add- Throws:
java.lang.InterruptedException- if interrupted while waiting.java.lang.NullPointerException- if the specified element is null.
-
offer
public boolean offer(E o, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
Inserts the specified element at the tail of this queue, waiting if necessary up to the specified wait time for space to become available.- Specified by:
offerin interfacejava.util.concurrent.BlockingQueue<E>- Parameters:
o- the element to addtimeout- how long to wait before giving up, in units of unitunit- a TimeUnit determining how to interpret the timeout parameter- Returns:
- true if successful, or false if the specified waiting time elapses before space is available.
- Throws:
java.lang.InterruptedException- if interrupted while waiting.java.lang.NullPointerException- if the specified element is null.
-
offer
public boolean offer(E o)
Inserts the specified element at the tail of this queue if possible, returning immediately if this queue is full.- Specified by:
offerin interfacejava.util.concurrent.BlockingQueue<E>- Specified by:
offerin interfacejava.util.Queue<E>- Parameters:
o- the element to add.- Returns:
- true if it was possible to add the element to this queue, else false
- Throws:
java.lang.NullPointerException- if the specified element is null
-
take
public E take() throws java.lang.InterruptedException
- Specified by:
takein interfacejava.util.concurrent.BlockingQueue<E>- Throws:
java.lang.InterruptedException
-
poll
public E poll(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
- Specified by:
pollin interfacejava.util.concurrent.BlockingQueue<E>- Throws:
java.lang.InterruptedException
-
remove
public boolean remove(java.lang.Object o)
-
toArray
public java.lang.Object[] toArray()
-
toArray
public <T> T[] toArray(T[] a)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.util.AbstractCollection<E>
-
clear
public void clear()
-
drainTo
public int drainTo(java.util.Collection<? super E> c)
- Specified by:
drainToin interfacejava.util.concurrent.BlockingQueue<E>
-
drainTo
public int drainTo(java.util.Collection<? super E> c, int maxElements)
- Specified by:
drainToin interfacejava.util.concurrent.BlockingQueue<E>
-
iterator
public java.util.Iterator<E> iterator()
Returns an iterator over the elements in this queue in proper sequence. The returned Iterator is a "weakly consistent" iterator that will never throwConcurrentModificationException, 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.
-
writeObject
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOExceptionSave the state to a stream (that is, serialize it).- Parameters:
s- the stream- Throws:
java.io.IOException
-
readObject
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, java.lang.ClassNotFoundExceptionReconstitute this queue instance from a stream (that is, deserialize it).- Parameters:
s- the stream- Throws:
java.io.IOExceptionjava.lang.ClassNotFoundException
-
-