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.
Author:
John Cairns

Date: 7/9/12

  • Constructor Summary

    Constructors
    Constructor
    Description
    ConcurrentStack(int size)
     
    ConcurrentStack(int size, SpinPolicy spinPolicy)
    construct a new stack of given capacity
  • Method Summary

    Modifier and Type
    Method
    Description
    final void
    clear the stack - does not null old references
    final boolean
    Linear search the stack for contains - not an efficient operation
    final boolean
     
    final N
    peek at the top of the stack
    final N
    pop()
    pop the next element off the stack
    final N
    pop(long time, TimeUnit unit)
    Pop an element from the stack, waiting if necessary if the stack is currently empty
    final N
    Pop an element from the stack, waiting as long as required for an element to become available on the stack
    final boolean
    push(N n)
    add an element to the stack, failing if the stack is unable to grow
    final boolean
    push(N n, long time, TimeUnit unit)
    Push an element on the stack, waiting if necessary if the stack is currently full
    final void
    Push an element on the stack waiting as long as required for space to become available
    final int
    how much available space in the stack
    final int
    Return the size of the stack

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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>