Class MemorySafeLinkedBlockingQueue<E>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.concurrent.BlockingQueue<E>, java.util.Queue<E>
    Direct Known Subclasses:
    MemorySafeTaskQueue

    public class MemorySafeLinkedBlockingQueue<E>
    extends java.util.concurrent.LinkedBlockingQueue<E>
    Can completely solve the OOM problem caused by LinkedBlockingQueue, does not depend on Instrumentation and is easier to use than MemoryLimitedLinkedBlockingQueue.
    See Also:
    Serialized Form
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int getMaxFreeMemory()
      get the max free memory.
      boolean hasRemainedMemory()
      determine if there is any remaining free memory.
      boolean offer​(E e)  
      boolean offer​(E e, long timeout, java.util.concurrent.TimeUnit unit)  
      void put​(E e)  
      void setMaxFreeMemory​(int maxFreeMemory)
      set the max free memory.
      void setRejector​(Rejector<E> rejector)
      set the rejector.
      • Methods inherited from class java.util.concurrent.LinkedBlockingQueue

        clear, contains, drainTo, drainTo, forEach, iterator, peek, poll, poll, remainingCapacity, remove, removeAll, removeIf, retainAll, size, spliterator, take, toArray, toArray, toString
      • Methods inherited from class java.util.AbstractQueue

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

        containsAll, isEmpty
      • 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
      • Methods inherited from interface java.util.Collection

        addAll, containsAll, equals, hashCode, isEmpty, parallelStream, stream, toArray
      • Methods inherited from interface java.util.Queue

        element, remove
    • Constructor Detail

      • MemorySafeLinkedBlockingQueue

        public MemorySafeLinkedBlockingQueue​(int maxFreeMemory)
      • MemorySafeLinkedBlockingQueue

        public MemorySafeLinkedBlockingQueue​(java.util.Collection<? extends E> c,
                                             int maxFreeMemory)
    • Method Detail

      • setMaxFreeMemory

        public void setMaxFreeMemory​(int maxFreeMemory)
        set the max free memory.
        Parameters:
        maxFreeMemory - the max free memory
      • getMaxFreeMemory

        public int getMaxFreeMemory()
        get the max free memory.
        Returns:
        the max free memory limit
      • setRejector

        public void setRejector​(Rejector<E> rejector)
        set the rejector.
        Parameters:
        rejector - the rejector
      • hasRemainedMemory

        public boolean hasRemainedMemory()
        determine if there is any remaining free memory.
        Returns:
        true if has free memory
      • put

        public void put​(E e)
                 throws java.lang.InterruptedException
        Specified by:
        put in interface java.util.concurrent.BlockingQueue<E>
        Overrides:
        put in class java.util.concurrent.LinkedBlockingQueue<E>
        Throws:
        java.lang.InterruptedException
      • offer

        public boolean offer​(E e,
                             long timeout,
                             java.util.concurrent.TimeUnit unit)
                      throws java.lang.InterruptedException
        Specified by:
        offer in interface java.util.concurrent.BlockingQueue<E>
        Overrides:
        offer in class java.util.concurrent.LinkedBlockingQueue<E>
        Throws:
        java.lang.InterruptedException
      • offer

        public boolean offer​(E e)
        Specified by:
        offer in interface java.util.concurrent.BlockingQueue<E>
        Specified by:
        offer in interface java.util.Queue<E>
        Overrides:
        offer in class java.util.concurrent.LinkedBlockingQueue<E>