Class ConcurrentStack<N>

java.lang.Object
com.conversantmedia.util.concurrent.ConcurrentStack<N>
All Implemented Interfaces:
Stack<N>, BlockingStack<N>

public final class ConcurrentStack<N> extends Object implements BlockingStack<N>
Concurrent "lock-free" version of a stack.
  • Field Details

  • Constructor Details

    • ConcurrentStack

      public ConcurrentStack(int size)
    • ConcurrentStack

      public ConcurrentStack(int size, SpinPolicy spinPolicy)
      construct a new stack of given capacity
      Parameters:
      size - - the stack size
      spinPolicy - - determine the level of cpu aggressiveness in waiting
  • Method Details

    • push

      public final boolean push(N n, long time, TimeUnit unit) throws InterruptedException
      Description copied from interface: BlockingStack
      Push an element on the stack, waiting if necessary if the stack is currently full
      Specified by:
      push in interface BlockingStack<N>
      Parameters:
      n - - the element to push on the stack
      time - - the maximum time to wait
      unit - - unit of waiting time
      Returns:
      boolean - true if item was pushed, false otherwise
      Throws:
      InterruptedException - on interrupt
    • pushInterruptibly

      public final void pushInterruptibly(N n) throws InterruptedException
      Description copied from interface: BlockingStack
      Push an element on the stack waiting as long as required for space to become available
      Specified by:
      pushInterruptibly in interface BlockingStack<N>
      Parameters:
      n - - the element to push
      Throws:
      InterruptedException - - in the event the current thread is interrupted prior to pushing the element
    • contains

      public final boolean contains(N n)
      Description copied from interface: Stack
      Linear search the stack for contains - not an efficient operation
      Specified by:
      contains in interface Stack<N>
      Parameters:
      n - - Object to test for containment
      Returns:
      boolean - true if n is contained somewhere in the stack
    • push

      public final boolean push(N n)
      add an element to the stack, failing if the stack is unable to grow
      Specified by:
      push in interface Stack<N>
      Parameters:
      n - - the element to push
      Returns:
      boolean - false if stack overflow, true otherwise
    • peek

      public final N peek()
      peek at the top of the stack
      Specified by:
      peek in interface Stack<N>
      Returns:
      N - the object at the top of the stack
    • pop

      public final N pop()
      pop the next element off the stack
      Specified by:
      pop in interface Stack<N>
      Returns:
      N - The object on the top of the stack
    • pop

      public final N pop(long time, TimeUnit unit) throws InterruptedException
      Description copied from interface: BlockingStack
      Pop an element from the stack, waiting if necessary if the stack is currently empty
      Specified by:
      pop in interface BlockingStack<N>
      Parameters:
      time - - the maximum time to wait
      unit - - the time unit for the waiting time
      Returns:
      N - the popped element, or null in the event of a timeout
      Throws:
      InterruptedException - on interrupt
    • popInterruptibly

      public final N popInterruptibly() throws InterruptedException
      Description copied from interface: BlockingStack
      Pop an element from the stack, waiting as long as required for an element to become available on the stack
      Specified by:
      popInterruptibly in interface BlockingStack<N>
      Returns:
      N - the popped element
      Throws:
      InterruptedException - - in the event the current thread is interrupted prior to popping any element
    • size

      public final int size()
      Return the size of the stack
      Specified by:
      size in interface Stack<N>
      Returns:
      int - number of elements in the stack
    • remainingCapacity

      public final int remainingCapacity()
      how much available space in the stack
      Specified by:
      remainingCapacity in interface Stack<N>
      Returns:
      int - the number of empty slots available in the stack
    • isEmpty

      public final boolean isEmpty()
      Specified by:
      isEmpty in interface Stack<N>
      Returns:
      boolean - true if stack is currently empty
    • clear

      public final void clear()
      clear the stack - does not null old references
      Specified by:
      clear in interface Stack<N>
    • isFull

      private boolean isFull()