Class VariableLinkedBlockingQueue<E>
- Type Parameters:
E
- the type of elements held in this collection
- All Implemented Interfaces:
Serializable
,Iterable<E>
,Collection<E>
,BlockingQueue<E>
,Queue<E>
setCapacity(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 Collection
and Iterator
interfaces.
This class is a member of the Java Collections Framework.
- Since:
- 1.5
- Author:
- Doug Lea
- See Also:
-
Constructor Summary
ConstructorDescriptionCreates a LinkedBlockingQueue with a capacity ofInteger.MAX_VALUE
.VariableLinkedBlockingQueue
(int capacity) Creates a LinkedBlockingQueue with the given (fixed) capacity.VariableLinkedBlockingQueue
(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
Modifier and TypeMethodDescriptionvoid
clear()
int
drainTo
(Collection<? super E> c) int
drainTo
(Collection<? super E> c, int maxElements) iterator()
Returns an iterator over the elements in this queue in proper sequence.boolean
Inserts the specified element at the tail of this queue if possible, returning immediately if this queue is full.boolean
Inserts the specified element at the tail of this queue, waiting if necessary up to the specified wait time for space to become available.peek()
poll()
void
Adds the specified element to the tail of this queue, waiting if necessary for space to become available.int
Returns the number of elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking.boolean
void
setCapacity
(int capacity) Set a new capacity for the queue.int
size()
Returns the number of elements in this queue.take()
Object[]
toArray()
<T> T[]
toArray
(T[] a) toString()
Methods inherited from class java.util.AbstractQueue
add, addAll, element, remove
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
Methods inherited from interface java.util.concurrent.BlockingQueue
add, contains
Methods inherited from interface java.util.Collection
addAll, containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray
-
Constructor Details
-
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:
IllegalArgumentException
- if capacity is not greater than zero.
-
VariableLinkedBlockingQueue
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:
NullPointerException
- if c or any element within it is null
-
-
Method Details
-
size
public int size()Returns the number of elements in this queue.- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in classAbstractCollection<E>
- 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:
remainingCapacity
in interfaceBlockingQueue<E>
-
put
Adds the specified element to the tail of this queue, waiting if necessary for space to become available.- Specified by:
put
in interfaceBlockingQueue<E>
- Parameters:
o
- the element to add- Throws:
InterruptedException
- if interrupted while waiting.NullPointerException
- if the specified element is null.
-
offer
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:
offer
in interfaceBlockingQueue<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:
InterruptedException
- if interrupted while waiting.NullPointerException
- if the specified element is null.
-
offer
Inserts the specified element at the tail of this queue if possible, returning immediately if this queue is full.- Specified by:
offer
in interfaceBlockingQueue<E>
- Specified by:
offer
in interfaceQueue<E>
- Parameters:
o
- the element to add.- Returns:
- true if it was possible to add the element to this queue, else false
- Throws:
NullPointerException
- if the specified element is null
-
take
- Specified by:
take
in interfaceBlockingQueue<E>
- Throws:
InterruptedException
-
poll
- Specified by:
poll
in interfaceBlockingQueue<E>
- Throws:
InterruptedException
-
poll
-
peek
-
remove
- Specified by:
remove
in interfaceBlockingQueue<E>
- Specified by:
remove
in interfaceCollection<E>
- Overrides:
remove
in classAbstractCollection<E>
-
toArray
- Specified by:
toArray
in interfaceCollection<E>
- Overrides:
toArray
in classAbstractCollection<E>
-
toArray
public <T> T[] toArray(T[] a) - Specified by:
toArray
in interfaceCollection<E>
- Overrides:
toArray
in classAbstractCollection<E>
-
toString
- Overrides:
toString
in classAbstractCollection<E>
-
clear
public void clear()- Specified by:
clear
in interfaceCollection<E>
- Overrides:
clear
in classAbstractQueue<E>
-
drainTo
- Specified by:
drainTo
in interfaceBlockingQueue<E>
-
drainTo
- Specified by:
drainTo
in interfaceBlockingQueue<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.- Specified by:
iterator
in interfaceCollection<E>
- Specified by:
iterator
in interfaceIterable<E>
- Specified by:
iterator
in classAbstractCollection<E>
- Returns:
- an iterator over the elements in this queue in proper sequence.
-