Class SizeBoundedQueue<T>


  • public class SizeBoundedQueue<T>
    extends java.lang.Object
    Blocking FIFO queue bounded by the max number of bytes of all elements. When adding threads are blocked due to capacity constraints, and the application terminates, it is the caller's duty to interrupt all threads.
    Since:
    4.0.4
    Author:
    Bela Ban
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected static class  SizeBoundedQueue.El<T>  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int count  
      protected boolean done  
      protected java.util.concurrent.locks.Lock lock  
      protected int max_size  
      protected java.util.concurrent.locks.Condition not_empty  
      protected java.util.concurrent.locks.Condition not_full  
      protected java.util.Queue<SizeBoundedQueue.El<T>> queue  
      protected int waiters  
    • Constructor Summary

      Constructors 
      Constructor Description
      SizeBoundedQueue​(int max_size)  
      SizeBoundedQueue​(int max_size, java.util.concurrent.locks.Lock lock)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(T element, int size)  
      void clear​(boolean done)  
      int drainTo​(java.util.Collection<T> c, int max_bytes)
      Removes and adds to collection c as many elements as possible (including waiters) but not exceeding max_bytes.
      int getElements()
      Returns the number of elements in the queue
      int getWaiters()  
      boolean hasWaiters()  
      boolean isDone()  
      boolean isEmpty()  
      int queueSize()
      For testing only - should always be the same as size()
      T remove()
      Removes and returns the first element or null if the queue is empty
      int size()
      Returns the accumulated size of all elements in the queue
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • lock

        protected final java.util.concurrent.locks.Lock lock
      • not_full

        protected final java.util.concurrent.locks.Condition not_full
      • not_empty

        protected final java.util.concurrent.locks.Condition not_empty
      • max_size

        protected final int max_size
      • count

        protected int count
      • waiters

        protected int waiters
      • done

        protected boolean done
    • Constructor Detail

      • SizeBoundedQueue

        public SizeBoundedQueue​(int max_size)
      • SizeBoundedQueue

        public SizeBoundedQueue​(int max_size,
                                java.util.concurrent.locks.Lock lock)
    • Method Detail

      • add

        public void add​(T element,
                        int size)
                 throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • remove

        public T remove()
        Removes and returns the first element or null if the queue is empty
      • drainTo

        public int drainTo​(java.util.Collection<T> c,
                           int max_bytes)
        Removes and adds to collection c as many elements as possible (including waiters) but not exceeding max_bytes. E.g. if we have elements {1000b, 2000b, 4000b} and max_bytes=6000, then only the first 2 elements are removed and the new size is 4000
        Parameters:
        c - The collection to transfer the removed elements to
        max_bytes - The max number of bytes to remove
        Returns:
        The accumulated number of bytes of all removed elements
      • clear

        public void clear​(boolean done)
      • getElements

        public int getElements()
        Returns the number of elements in the queue
      • size

        public int size()
        Returns the accumulated size of all elements in the queue
      • isEmpty

        public boolean isEmpty()
      • getWaiters

        public int getWaiters()
      • hasWaiters

        public boolean hasWaiters()
      • isDone

        public boolean isDone()
      • queueSize

        public int queueSize()
        For testing only - should always be the same as size()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object