public class ConcurrentEvictingQueue<E>
extends java.util.AbstractQueue<E>
ConcurrentEvictingQueue is already full ConcurrentEvictingQueue.size() == capacity, the
oldest element (the head) will be evicted, and then the new element added at the tail.
In order to achieve thread-safety it utilizes capability-based locking features of StampedLock. All spins optimistic/pessimistic reads and writes are encapsulated in following
methods:
All other logic just relies on this utility methods.
Also please take into account that size
and modificationsCount are volatile fields,
so we can read them and compare against them without any additional synchronizations.
This class IS thread-safe, and does NOT accept null elements.
| Constructor and Description |
|---|
ConcurrentEvictingQueue(int capacity) |
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Atomically removes all of the elements from this queue.
|
java.util.Iterator<E> |
iterator()
Returns an iterator over the elements in this queue in proper sequence.
|
boolean |
offer(E e)
Inserts the specified element at the tail of this queue if it is possible to do so
immediately or if capacity limit is exited the oldest element (the head) will be evicted, and
then the new element added at the tail.
|
E |
peek() |
E |
poll() |
int |
size()
Returns the number of elements in this queue.
|
java.lang.Object[] |
toArray()
Returns an array containing all of the elements in this queue, in proper sequence.
|
<T> T[] |
toArray(T[] destination)
Returns an array containing all of the elements in this queue, in proper sequence; the
runtime type of the returned array is that of the specified array.
|
contains, containsAll, isEmpty, remove, removeAll, retainAll, toStringclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitpublic java.util.Iterator<E> iterator()
This iterator implementation NOT allow removes and co-modifications.
public int size()
public boolean offer(E e)
AbstractQueue.add(E), which can fail to insert an element only by throwing an exception.java.lang.NullPointerException - if the specified element is nullpublic E poll()
public E peek()
public void clear()
public java.lang.Object[] toArray()
The returned array will be "safe" in that no references to it are maintained by this queue. (In other words, this method must allocate a new array). The caller is free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
public <T> T[] toArray(T[] destination)
Like the toArray() method, this method acts as bridge between
array-based and collection-based APIs. Further, this method allows precise control over the
runtime type of the output array, and may, under certain circumstances, be used to save
allocation costs.
Note that toArray(new Object[0]) is identical in function to toArray().
toArray in interface java.util.Collection<E>toArray in class java.util.AbstractCollection<E>destination - the array into which the elements of the queue are to be stored, if it is
big enough; otherwise, a new array of the same runtime type is allocated
for this purposejava.lang.ArrayStoreException - if the runtime type of the specified array is not a supertype of
the runtime type of every element in this queuejava.lang.NullPointerException - if the specified array is null