E
- The type of elements in the queue.public class ClosableBlockingQueue<E> extends Object
Note: Null elements are prohibited.
Constructor and Description |
---|
ClosableBlockingQueue()
Creates a new empty queue.
|
ClosableBlockingQueue(Collection<? extends E> initialElements)
Creates a new queue that contains the given elements.
|
ClosableBlockingQueue(int initialSize)
Creates a new empty queue, reserving space for at least the specified number
of elements.
|
Modifier and Type | Method and Description |
---|---|
void |
add(E element)
Adds the element to the queue, or fails with an exception, if the queue is closed.
|
boolean |
addIfOpen(E element)
Tries to add an element to the queue, if the queue is still open.
|
boolean |
close()
Tries to close the queue.
|
boolean |
equals(Object obj) |
List<E> |
getBatchBlocking()
Gets all the elements found in the list, or blocks until at least one element
was added.
|
List<E> |
getBatchBlocking(long timeoutMillis)
Gets all the elements found in the list, or blocks until at least one element
was added.
|
E |
getElementBlocking()
Returns the next element in the queue.
|
E |
getElementBlocking(long timeoutMillis)
Returns the next element in the queue.
|
int |
hashCode() |
boolean |
isEmpty()
Checks whether the queue is empty (has no elements).
|
boolean |
isOpen()
Checks whether the queue is currently open, meaning elements can be added and polled.
|
E |
peek()
Returns the queue's next element without removing it, if the queue is non-empty.
|
E |
poll()
Returns the queue's next element and removes it, the queue is non-empty.
|
List<E> |
pollBatch()
Returns all of the queue's current elements in a list, if the queue is non-empty.
|
int |
size()
Gets the number of elements currently in the queue.
|
String |
toString() |
public ClosableBlockingQueue()
public ClosableBlockingQueue(int initialSize)
initialSize
- The number of elements to reserve space for.public ClosableBlockingQueue(Collection<? extends E> initialElements)
initialElements
- The elements to initially add to the queue.public int size()
public boolean isEmpty()
public boolean isOpen()
public boolean close()
public boolean addIfOpen(E element)
Unlike the add(Object)
method, this method never throws an exception,
but only indicates via the return code if the element was added or the
queue was closed.
element
- The element to add.public void add(E element) throws IllegalStateException
element
- The element to add.IllegalStateException
- Thrown, if the queue is closed.public E peek()
The method throws an IllegalStateException
if the queue is closed.
Checking whether the queue is open and getting the next element is one atomic operation.
This method never blocks.
IllegalStateException
- Thrown, if the queue is closed.public E poll()
The method throws an IllegalStateException
if the queue is closed.
Checking whether the queue is open and removing the next element is one atomic operation.
This method never blocks.
IllegalStateException
- Thrown, if the queue is closed.public List<E> pollBatch()
The method throws an IllegalStateException
if the queue is closed.
Checking whether the queue is open and removing the elements is one atomic operation.
This method never blocks.
IllegalStateException
- Thrown, if the queue is closed.public E getElementBlocking() throws InterruptedException
The method throws an IllegalStateException
if the queue is closed.
Checking whether the queue is open and removing the next element is one atomic operation.
IllegalStateException
- Thrown, if the queue is closed.InterruptedException
- Throw, if the thread is interrupted while waiting for an
element to be added.public E getElementBlocking(long timeoutMillis) throws InterruptedException
The method throws an IllegalStateException
if the queue is closed.
Checking whether the queue is open and removing the next element is one atomic operation.
timeoutMillis
- The number of milliseconds to block, at most.IllegalStateException
- Thrown, if the queue is closed.InterruptedException
- Throw, if the thread is interrupted while waiting for an
element to be added.public List<E> getBatchBlocking() throws InterruptedException
This method always returns a list with at least one element.
The method throws an IllegalStateException
if the queue is closed.
Checking whether the queue is open and removing the next element is one atomic operation.
IllegalStateException
- Thrown, if the queue is closed.InterruptedException
- Throw, if the thread is interrupted while waiting for an
element to be added.public List<E> getBatchBlocking(long timeoutMillis) throws InterruptedException
getBatchBlocking()
, but takes
a number of milliseconds that the method will maximally wait before returning.
This method never returns null, but an empty list, if the queue is empty when the method is called and the request times out before an element was added.
The method throws an IllegalStateException
if the queue is closed.
Checking whether the queue is open and removing the next element is one atomic operation.
timeoutMillis
- The number of milliseconds to wait, at most.IllegalStateException
- Thrown, if the queue is closed.InterruptedException
- Throw, if the thread is interrupted while waiting for an
element to be added.Copyright © 2014–2017 The Apache Software Foundation. All rights reserved.