Class ConcurrentEvictingQueue<E>
- All Implemented Interfaces:
Iterable<E>,Collection<E>,Queue<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:
-
readConcurrently(Supplier) -
readConcurrentlyWithoutSpin(Supplier) -
writeConcurrently(Supplier)
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Atomically removes all of the elements from this queue.iterator()Returns an iterator over the elements in this queue in proper sequence.booleanInserts 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.peek()poll()intsize()Returns the number of elements in this queue.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.Methods inherited from class java.util.AbstractQueue
add, addAll, element, removeMethods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, remove, removeAll, retainAll, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
contains, containsAll, equals, hashCode, isEmpty, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray
-
Constructor Details
-
ConcurrentEvictingQueue
public ConcurrentEvictingQueue(int capacity)
-
-
Method Details
-
iterator
Returns an iterator over the elements in this queue in proper sequence. The elements will be returned in order from first (head) to last (tail).This iterator implementation NOT allow removes and co-modifications.
- Specified by:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin classAbstractCollection<E>- Returns:
- an iterator over the elements in this queue in proper sequence
-
size
public int size()Returns the number of elements in this queue.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- the number of elements in this queue
-
offer
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. This method is generally preferable to methodAbstractQueue.add(E), which can fail to insert an element only by throwing an exception.- Throws:
NullPointerException- if the specified element is null
-
poll
-
peek
-
clear
public void clear()Atomically removes all of the elements from this queue. The queue will be empty after this call returns.- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractQueue<E>
-
toArray
Returns an array containing all of the elements in this queue, in proper sequence.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.
- Specified by:
toArrayin interfaceCollection<E>- Overrides:
toArrayin classAbstractCollection<E>- Returns:
- an array containing all of the elements in this queue
-
toArray
public <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. If the queue fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this queue.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 totoArray().- Specified by:
toArrayin interfaceCollection<E>- Overrides:
toArrayin classAbstractCollection<E>- Parameters:
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 purpose- Returns:
- an array containing all of the elements in this queue
- Throws:
ArrayStoreException- if the runtime type of the specified array is not a supertype of the runtime type of every element in this queueNullPointerException- if the specified array is null
-