Class ConcurrentStack<N>
java.lang.Object
com.conversantmedia.util.concurrent.ConcurrentStack<N>
- All Implemented Interfaces:
Stack<N>, BlockingStack<N>
Concurrent "lock-free" version of a stack.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate final classprivate final classprivate final classprivate final classprivate final classprivate final class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final SequenceLockprivate final intprivate final AtomicReferenceArray<N> private final Conditionprivate final Conditionprivate final ContendedAtomicInteger -
Constructor Summary
ConstructorsConstructorDescriptionConcurrentStack(int size) ConcurrentStack(int size, SpinPolicy spinPolicy) construct a new stack of given capacity -
Method Summary
Modifier and TypeMethodDescriptionfinal voidclear()clear the stack - does not null old referencesfinal booleanLinear search the stack for contains - not an efficient operationfinal booleanisEmpty()private booleanisFull()final Npeek()peek at the top of the stackfinal Npop()pop the next element off the stackfinal NPop an element from the stack, waiting if necessary if the stack is currently emptyfinal NPop an element from the stack, waiting as long as required for an element to become available on the stackfinal booleanadd an element to the stack, failing if the stack is unable to growfinal booleanPush an element on the stack, waiting if necessary if the stack is currently fullfinal voidPush an element on the stack waiting as long as required for space to become availablefinal inthow much available space in the stackfinal intsize()Return the size of the stack
-
Field Details
-
size
private final int size -
stack
-
stackTop
-
seqLock
-
stackNotFullCondition
-
stackNotEmptyCondition
-
-
Constructor Details
-
ConcurrentStack
public ConcurrentStack(int size) -
ConcurrentStack
construct a new stack of given capacity- Parameters:
size- - the stack sizespinPolicy- - determine the level of cpu aggressiveness in waiting
-
-
Method Details
-
push
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
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
Description copied from interface:StackLinear search the stack for contains - not an efficient operation -
push
-
peek
-
pop
-
pop
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
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
-
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
-
clear
-
isFull
private boolean isFull()
-