Class ConcurrentEvictingQueue<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractQueue<E>
-
- io.github.resilience4j.circularbuffer.ConcurrentEvictingQueue<E>
-
- All Implemented Interfaces:
java.lang.Iterable<E>,java.util.Collection<E>,java.util.Queue<E>
public class ConcurrentEvictingQueue<E> extends java.util.AbstractQueue<E>The purpose of this queue is to store the N most recently inserted elements. If theConcurrentEvictingQueueis already fullConcurrentEvictingQueue.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
sizeandmodificationsCountarevolatilefields, 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 Constructor Description ConcurrentEvictingQueue(int capacity)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()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.booleanoffer(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.Epeek()Epoll()intsize()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.-
Methods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, remove, removeAll, retainAll, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
-
-
-
Method Detail
-
iterator
public java.util.Iterator<E> 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.
-
size
public int size()
Returns the number of elements in this queue.
-
offer
public 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. This method is generally preferable to methodAbstractQueue.add(E), which can fail to insert an element only by throwing an exception.- Throws:
java.lang.NullPointerException- if the specified element is null
-
poll
public E poll()
-
peek
public E peek()
-
clear
public void clear()
Atomically removes all of the elements from this queue. The queue will be empty after this call returns.
-
toArray
public java.lang.Object[] 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.
-
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 interfacejava.util.Collection<E>- Overrides:
toArrayin classjava.util.AbstractCollection<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:
java.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
-
-