Class ConcurrentEvictingQueue<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
io.github.resilience4j.circularbuffer.ConcurrentEvictingQueue<E>
All Implemented Interfaces:
Iterable<E>, Collection<E>, Queue<E>

public class ConcurrentEvictingQueue<E> extends AbstractQueue<E>
The purpose of this queue is to store the N most recently inserted elements. If the 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 Details

    • ConcurrentEvictingQueue

      public ConcurrentEvictingQueue(int capacity)
  • Method Details

    • iterator

      public 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.

      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in class AbstractCollection<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:
      size in interface Collection<E>
      Specified by:
      size in class AbstractCollection<E>
      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 method AbstractQueue.add(E), which can fail to insert an element only by throwing an exception.
      Throws:
      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.
      Specified by:
      clear in interface Collection<E>
      Overrides:
      clear in class AbstractQueue<E>
    • toArray

      public 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.

      Specified by:
      toArray in interface Collection<E>
      Overrides:
      toArray in class AbstractCollection<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 to toArray().

      Specified by:
      toArray in interface Collection<E>
      Overrides:
      toArray in class 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:
      ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this queue
      NullPointerException - if the specified array is null