Class ConcurrentLinkedBlockingQueue2<T>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<T>, java.util.Collection<T>, java.util.concurrent.BlockingQueue<T>, java.util.Queue<T>

    public class ConcurrentLinkedBlockingQueue2<T>
    extends java.util.concurrent.ConcurrentLinkedQueue<T>
    implements java.util.concurrent.BlockingQueue<T>
    Attempt at writing a fast transfer queue, which is bounded. The take() method blocks until there is an element, but the offer() method drops the element and returns if the queue is full (doesn't block).

    The design assumes a number of producers but only one consumer. The consumer only blocks when the queue is empty (on the not-empty condition), the producers block when the queue is full (on the not-full condition). The producers increment a count atomically and if the count is greater than the capacity, they block on the not-full condition. The consumer decrements the condition and signals the not-full condition when the count is capacity -1 (from capacity to capacity-1). The producers signal not-empty when the count is 1 (from 0 to 1)

    Since:
    3.5
    Author:
    Bela Ban
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int capacity  
      protected java.util.concurrent.atomic.AtomicInteger count  
      protected java.util.concurrent.locks.Condition not_empty  
      protected java.util.concurrent.locks.Lock not_empty_lock  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void decrCount()  
      int drainTo​(java.util.Collection<? super T> c)  
      int drainTo​(java.util.Collection<? super T> c, int maxElements)  
      protected void incrCount()  
      boolean offer​(T t)
      Drops elements if capacity has been reached.
      boolean offer​(T t, long timeout, java.util.concurrent.TimeUnit unit)  
      T poll()  
      T poll​(long timeout, java.util.concurrent.TimeUnit unit)  
      void put​(T t)  
      int remainingCapacity()  
      boolean remove​(java.lang.Object o)  
      int size()  
      T take()  
      protected void waitForNotEmpty()  
      • Methods inherited from class java.util.concurrent.ConcurrentLinkedQueue

        add, addAll, clear, contains, forEach, isEmpty, iterator, peek, removeAll, removeIf, retainAll, spliterator, toArray, toArray, toString
      • Methods inherited from class java.util.AbstractQueue

        element, remove
      • Methods inherited from class java.util.AbstractCollection

        containsAll
      • 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

        add, contains
      • Methods inherited from interface java.util.Collection

        addAll, clear, containsAll, equals, hashCode, isEmpty, iterator, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.Queue

        element, peek, remove
    • Field Detail

      • capacity

        protected final int capacity
      • count

        protected final java.util.concurrent.atomic.AtomicInteger count
      • not_empty_lock

        protected final java.util.concurrent.locks.Lock not_empty_lock
      • not_empty

        protected final java.util.concurrent.locks.Condition not_empty
    • Constructor Detail

      • ConcurrentLinkedBlockingQueue2

        public ConcurrentLinkedBlockingQueue2​(int capacity)
    • Method Detail

      • offer

        public boolean offer​(T t)
        Drops elements if capacity has been reached. That's OK for the ThreadPoolExecutor as dropped messages will get retransmitted
        Specified by:
        offer in interface java.util.concurrent.BlockingQueue<T>
        Specified by:
        offer in interface java.util.Queue<T>
        Overrides:
        offer in class java.util.concurrent.ConcurrentLinkedQueue<T>
        Parameters:
        t -
        Returns:
      • take

        public T take()
               throws java.lang.InterruptedException
        Specified by:
        take in interface java.util.concurrent.BlockingQueue<T>
        Throws:
        java.lang.InterruptedException
      • poll

        public T poll()
        Specified by:
        poll in interface java.util.Queue<T>
        Overrides:
        poll in class java.util.concurrent.ConcurrentLinkedQueue<T>
      • poll

        public T poll​(long timeout,
                      java.util.concurrent.TimeUnit unit)
               throws java.lang.InterruptedException
        Specified by:
        poll in interface java.util.concurrent.BlockingQueue<T>
        Throws:
        java.lang.InterruptedException
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.concurrent.BlockingQueue<T>
        Specified by:
        remove in interface java.util.Collection<T>
        Overrides:
        remove in class java.util.concurrent.ConcurrentLinkedQueue<T>
      • remainingCapacity

        public int remainingCapacity()
        Specified by:
        remainingCapacity in interface java.util.concurrent.BlockingQueue<T>
      • drainTo

        public int drainTo​(java.util.Collection<? super T> c)
        Specified by:
        drainTo in interface java.util.concurrent.BlockingQueue<T>
      • put

        public void put​(T t)
                 throws java.lang.InterruptedException
        Specified by:
        put in interface java.util.concurrent.BlockingQueue<T>
        Throws:
        java.lang.InterruptedException
      • offer

        public boolean offer​(T t,
                             long timeout,
                             java.util.concurrent.TimeUnit unit)
                      throws java.lang.InterruptedException
        Specified by:
        offer in interface java.util.concurrent.BlockingQueue<T>
        Throws:
        java.lang.InterruptedException
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<T>
        Overrides:
        size in class java.util.concurrent.ConcurrentLinkedQueue<T>
      • drainTo

        public int drainTo​(java.util.Collection<? super T> c,
                           int maxElements)
        Specified by:
        drainTo in interface java.util.concurrent.BlockingQueue<T>
      • waitForNotEmpty

        protected void waitForNotEmpty()
                                throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • decrCount

        protected void decrCount()
      • incrCount

        protected void incrCount()