Class SynchronizedStack<T>

  • Type Parameters:
    T - The type of object managed by this stack
    Direct Known Subclasses:
    AbstractProtocol.RecycledProcessors

    public class SynchronizedStack<T>
    extends java.lang.Object
    This is intended as a (mostly) GC-free alternative to Stack when the requirement is to create a pool of re-usable objects with no requirement to shrink the pool. The aim is to provide the bare minimum of required functionality as quickly as possible with minimum garbage.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_SIZE
      The default initial capacity for a new stack.
    • Constructor Summary

      Constructors 
      Constructor Description
      SynchronizedStack()
      Constructs a new SynchronizedStack with default size and no limit.
      SynchronizedStack​(int size, int limit)
      Constructs a new SynchronizedStack with the specified initial size and limit.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears all objects from the stack.
      T pop()
      Pops an object from the stack.
      boolean push​(T obj)
      Pushes an object onto the stack.
      void setLimit​(int limit)
      Sets the maximum number of elements that can be pushed onto this stack.
      • Methods inherited from class java.lang.Object

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

      • DEFAULT_SIZE

        public static final int DEFAULT_SIZE
        The default initial capacity for a new stack.
        See Also:
        Constant Field Values
    • Constructor Detail

      • SynchronizedStack

        public SynchronizedStack()
        Constructs a new SynchronizedStack with default size and no limit.
      • SynchronizedStack

        public SynchronizedStack​(int size,
                                 int limit)
        Constructs a new SynchronizedStack with the specified initial size and limit.
        Parameters:
        size - the initial capacity
        limit - the maximum capacity, or -1 for unlimited
    • Method Detail

      • push

        public boolean push​(T obj)
        Pushes an object onto the stack.
        Parameters:
        obj - The object to push
        Returns:
        true if the object was pushed, false if the stack is full
      • pop

        public T pop()
        Pops an object from the stack.
        Returns:
        the object at the top of the stack, or null if the stack is empty
      • clear

        public void clear()
        Clears all objects from the stack.
      • setLimit

        public void setLimit​(int limit)
        Sets the maximum number of elements that can be pushed onto this stack. If the new limit is lower than the current capacity, the stack is shrunk. When the stack already holds more elements than the new limit, the most recently pushed elements are retained and the remaining elements are discarded.
        Parameters:
        limit - the maximum size, or -1 for unlimited