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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()clear the stack - does not null old referencesbooleancontains(N n)Linear search the stack for contains - not an efficient operationbooleanisEmpty()Npeek()peek at the top of the stackNpop()pop the next element off the stackNpop(long time, TimeUnit unit)Pop an element from the stack, waiting if necessary if the stack is currently emptyNpopInterruptibly()Pop an element from the stack, waiting as long as required for an element to become available on the stackbooleanpush(N n)add an element to the stack, failing if the stack is unable to growbooleanpush(N n, long time, TimeUnit unit)Push an element on the stack, waiting if necessary if the stack is currently fullvoidpushInterruptibly(N n)Push an element on the stack waiting as long as required for space to become availableintremainingCapacity()how much available space in the stackintsize()Return the size of the stack
-
-
-
Constructor Detail
-
ConcurrentStack
public ConcurrentStack(int size)
-
ConcurrentStack
public ConcurrentStack(int size, SpinPolicy spinPolicy)construct a new stack of given capacity- Parameters:
size- - the stack sizespinPolicy- - determine the level of cpu aggressiveness in waiting
-
-
Method Detail
-
push
public final boolean push(N n, long time, TimeUnit unit) throws InterruptedException
Description copied from interface:BlockingStackPush an element on the stack, waiting if necessary if the stack is currently full- Specified by:
pushin interfaceBlockingStack<N>- Parameters:
n- - the element to push on the stacktime- - the maximum time to waitunit- - 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:BlockingStackPush an element on the stack waiting as long as required for space to become available- Specified by:
pushInterruptiblyin interfaceBlockingStack<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:StackLinear search the stack for contains - not an efficient operation
-
push
public final boolean push(N n)
add an element to the stack, failing if the stack is unable to grow
-
peek
public final N peek()
peek at the top of the stack
-
pop
public final N pop()
pop the next element off the stack
-
pop
public final N pop(long time, TimeUnit unit) throws InterruptedException
Description copied from interface:BlockingStackPop an element from the stack, waiting if necessary if the stack is currently empty- Specified by:
popin interfaceBlockingStack<N>- Parameters:
time- - the maximum time to waitunit- - 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:BlockingStackPop an element from the stack, waiting as long as required for an element to become available on the stack- Specified by:
popInterruptiblyin interfaceBlockingStack<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
-
remainingCapacity
public final int remainingCapacity()
how much available space in the stack- Specified by:
remainingCapacityin interfaceStack<N>- Returns:
- int - the number of empty slots available in the stack
-
isEmpty
public final boolean isEmpty()
-
-