Package org.ka2ddo.util
Class FastBlockingQueue<E>
java.lang.Object
java.util.AbstractCollection<E>
org.ka2ddo.util.FastBlockingQueue<E>
- Type Parameters:
E
- data type of queued records
- All Implemented Interfaces:
Serializable
,Iterable<E>
,Collection<E>
,BlockingQueue<E>
,Queue<E>
- Direct Known Subclasses:
GenericTaggedQueue
public class FastBlockingQueue<E>
extends AbstractCollection<E>
implements BlockingQueue<E>, Serializable
This attempts to make a simpler and faster FIFO queue than ArrayBlockingQueue with
no guarantees regarding fairness, minimum execution time, and minimum transient
memory allocations. Optimized for single producer and single consumer.
- Author:
- Andrew Pavlin, KA2DDO
- See Also:
-
Constructor Summary
ConstructorDescriptionFastBlockingQueue
(int capacity) Create a FastBlockingQueue with the specified maximum queue backlog. -
Method Summary
Modifier and TypeMethodDescriptionboolean
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
Check if this queue contains the specified object (or at least an instance that matches by equals()).int
Removes at most the given number of available elements from this queue and adds them to the given array.int
drainTo
(Collection<? super E> c) Removes all available elements from this queue and adds them to the given collection.int
drainTo
(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.element()
Retrieves, but does not remove, the head of this queue.void
expandCapacity
(int capacity) Enlarge the queue's backlog capacity.boolean
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.final int
fastSize()
Returns the number of elements in this queue, not using synchronization to save time.int
Report the total number of queue slots in the queue.iterator()
Returns an iterator over the elements contained in this collection.boolean
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.boolean
Inserts the specified element into this queue, waiting up to the specified wait time if necessary for space to become available.peek()
Retrieves, but does not remove, the head of this queue, or returnsnull
if this queue is empty.poll()
Retrieves and removes the head of this queue, or returnsnull
if this queue is empty.Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.void
Inserts the specified element into this queue, waiting if necessary for space to become available.void
Inserts all the non-null elements in the specified array into this queue, waiting if necessary for sufficient space to become available.int
Returns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking, orInteger.MAX_VALUE
if there is no intrinsic limit.remove()
Retrieves and removes the head of this queue.int
size()
Returns the number of elements in this queue.take()
Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
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
remove
Methods inherited from interface java.util.Collection
addAll, containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
Constructor Details
-
FastBlockingQueue
public FastBlockingQueue(int capacity) Create a FastBlockingQueue with the specified maximum queue backlog.- Parameters:
capacity
- int maximum capacity of queue
-
-
Method Details
-
clear
public void clear()Remove everything from the queue.- Specified by:
clear
in interfaceCollection<E>
- Overrides:
clear
in classAbstractCollection<E>
-
drainTo
Removes all available elements from this queue and adds them to the given collection. A failure encountered while attempting to add elements to collectionc
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 inIllegalArgumentException
. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.- Specified by:
drainTo
in interfaceBlockingQueue<E>
- Parameters:
c
- the collection to transfer elements into- Returns:
- the number of elements transferred
- Throws:
UnsupportedOperationException
- if addition of elements is not supported by the specified collectionClassCastException
- if the class of an element of this queue prevents it from being added to the specified collectionNullPointerException
- if the specified collection is nullIllegalArgumentException
- if the specified collection is this queue, or some property of an element of this queue prevents it from being added to the specified collection
-
iterator
Returns an iterator over the elements contained in this collection. Note this iterator is not thread-safe and thereby an iterator instance should only be used by one thread.- Specified by:
iterator
in interfaceCollection<E>
- Specified by:
iterator
in interfaceIterable<E>
- Specified by:
iterator
in classAbstractCollection<E>
- Returns:
- an iterator over the elements contained in this collection
-
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 collection
-
fastSize
public final int fastSize()Returns the number of elements in this queue, not using synchronization to save time. As such, the answer may be inaccurate, but it will be obtained quicker.- Returns:
- the number of elements in this collection
-
put
Inserts the specified element into this queue, waiting if necessary for space to become available.- Specified by:
put
in interfaceBlockingQueue<E>
- Parameters:
e
- the element to add- Throws:
InterruptedException
- if interrupted while waitingClassCastException
- if the class of the specified element prevents it from being added to this queueNullPointerException
- if the specified element is nullIllegalArgumentException
- if some property of the specified element prevents it from being added to this queue
-
putAll
Inserts all the non-null elements in the specified array into this queue, waiting if necessary for sufficient space to become available.- Parameters:
e
- the array of elements to addlength
- the int length of the array to test (not all of array may be in use)- Throws:
InterruptedException
- if interrupted while waitingClassCastException
- if the class of the specified element prevents it from being added to this queueNullPointerException
- if the specified element is nullIllegalArgumentException
- if some property of the specified element prevents it from being added to this queue
-
offer
Inserts the specified element into this queue, waiting up to the specified wait time if necessary for space to become available.- Specified by:
offer
in interfaceBlockingQueue<E>
- Parameters:
e
- the element to addtimeout
- how long to wait before giving up, in units ofunit
unit
- aTimeUnit
determining how to interpret thetimeout
parameter- Returns:
true
if successful, orfalse
if the specified waiting time elapses before space is available- Throws:
InterruptedException
- if interrupted while waitingClassCastException
- if the class of the specified element prevents it from being added to this queueNullPointerException
- if the specified element is nullIllegalArgumentException
- if some property of the specified element prevents it from being added to this queue
-
take
Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.- Specified by:
take
in interfaceBlockingQueue<E>
- Returns:
- the head of this queue
- Throws:
InterruptedException
- if interrupted while waiting
-
poll
Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.- Specified by:
poll
in interfaceBlockingQueue<E>
- Parameters:
timeout
- how long to wait before giving up, in units ofunit
unit
- aTimeUnit
determining how to interpret thetimeout
parameter- Returns:
- the head of this queue, or
null
if the specified waiting time elapses before an element is available - Throws:
InterruptedException
- if interrupted while waiting
-
remainingCapacity
public int remainingCapacity()Returns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking, orInteger.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.- Specified by:
remainingCapacity
in interfaceBlockingQueue<E>
- Returns:
- the remaining capacity
-
drainTo
Removes at most the given number of available elements from this queue and adds them to the given collection. A failure encountered while attempting to add elements to collectionc
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 inIllegalArgumentException
. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.- Specified by:
drainTo
in interfaceBlockingQueue<E>
- Parameters:
c
- the collection to transfer elements intomaxElements
- the maximum number of elements to transfer- Returns:
- the number of elements transferred
- Throws:
UnsupportedOperationException
- if addition of elements is not supported by the specified collectionClassCastException
- if the class of an element of this queue prevents it from being added to the specified collectionNullPointerException
- if the specified collection is nullIllegalArgumentException
- if the specified collection is this queue, or some property of an element of this queue prevents it from being added to the specified collection
-
drainTo
Removes at most the given number of available elements from this queue and adds them to the given array. A failure encountered while attempting to add elements to arraya
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.- Parameters:
a
- the array to transfer elements into- Returns:
- the number of elements transferred
- Throws:
UnsupportedOperationException
- if addition of elements is not supported by the specified collectionClassCastException
- if the class of an element of this queue prevents it from being added to the specified collectionNullPointerException
- if the specified collection is nullIllegalArgumentException
- if the specified collection is this queue, or some property of an element of this queue prevents it from being added to the specified collection
-
offer
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable toadd(E)
, which can fail to insert an element only by throwing an exception.- Specified by:
offer
in interfaceBlockingQueue<E>
- Specified by:
offer
in interfaceQueue<E>
- Parameters:
e
- the element to add- Returns:
true
if the element was added to this queue, elsefalse
- Throws:
ClassCastException
- if the class of the specified element prevents it from being added to this queueNullPointerException
- if the specified element is null and this queue does not permit null elementsIllegalArgumentException
- if some property of this element prevents it from being added to this queue
-
fastOffer
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable toadd(E)
, which can fail to insert an element only by throwing an exception.- Parameters:
e
- the element to add- Returns:
true
if the element was added to this queue, elsefalse
- Throws:
ClassCastException
- if the class of the specified element prevents it from being added to this queueNullPointerException
- if the specified element is null and this queue does not permit null elementsIllegalArgumentException
- if some property of this element prevents it from being added to this queue
-
add
Add an element to the queue, throwing an exception if the queue is full and has no more room.- Specified by:
add
in interfaceBlockingQueue<E>
- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceQueue<E>
- Overrides:
add
in classAbstractCollection<E>
- Throws:
IllegalStateException
- if queue is already full
-
poll
Retrieves and removes the head of this queue, or returnsnull
if this queue is empty. -
peek
Retrieves, but does not remove, the head of this queue, or returnsnull
if this queue is empty. -
element
Retrieves, but does not remove, the head of this queue. This method differs frompeek
only in that it throws an exception if this queue is empty.This implementation returns the result of
peek
unless the queue is empty.- Specified by:
element
in interfaceQueue<E>
- Returns:
- the head of this queue
- Throws:
NoSuchElementException
- if this queue is empty
-
remove
Retrieves and removes the head of this queue. This method differs frompoll
only in that it throws an exception if this queue is empty.This implementation returns the result of
poll
unless the queue is empty.- Specified by:
remove
in interfaceQueue<E>
- Returns:
- the head of this queue
- Throws:
NoSuchElementException
- if this queue is empty
-
contains
Check if this queue contains the specified object (or at least an instance that matches by equals()).- Specified by:
contains
in interfaceBlockingQueue<E>
- Specified by:
contains
in interfaceCollection<E>
- Overrides:
contains
in classAbstractCollection<E>
-
getCapacity
public int getCapacity()Report the total number of queue slots in the queue.- Returns:
- maximum backlog capacity of the queue
-
expandCapacity
public void expandCapacity(int capacity) Enlarge the queue's backlog capacity. Note this is a no-op if the specified parameter is less than or equal to the current capacity of the queue.- Parameters:
capacity
- the new backlog capacity for the queue
-