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 class
private final class
private final class
private final class
private final class
private final class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final SequenceLock
private final int
private final AtomicReferenceArray
<N> private final Condition
private final Condition
private final ContendedAtomicInteger
-
Constructor Summary
ConstructorsConstructorDescriptionConcurrentStack
(int size) ConcurrentStack
(int size, SpinPolicy spinPolicy) construct a new stack of given capacity -
Method Summary
Modifier and TypeMethodDescriptionfinal void
clear()
clear the stack - does not null old referencesfinal boolean
Linear search the stack for contains - not an efficient operationfinal boolean
isEmpty()
private boolean
isFull()
final N
peek()
peek at the top of the stackfinal N
pop()
pop the next element off the stackfinal N
Pop an element from the stack, waiting if necessary if the stack is currently emptyfinal N
Pop an element from the stack, waiting as long as required for an element to become available on the stackfinal boolean
add an element to the stack, failing if the stack is unable to growfinal boolean
Push an element on the stack, waiting if necessary if the stack is currently fullfinal void
Push an element on the stack waiting as long as required for space to become availablefinal int
how much available space in the stackfinal int
size()
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:BlockingStack
Push an element on the stack, waiting if necessary if the stack is currently full- Specified by:
push
in 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:BlockingStack
Push an element on the stack waiting as long as required for space to become available- Specified by:
pushInterruptibly
in 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:Stack
Linear search the stack for contains - not an efficient operation -
push
add an element to the stack, failing if the stack is unable to grow -
peek
peek at the top of the stack -
pop
pop the next element off the stack -
pop
Description copied from interface:BlockingStack
Pop an element from the stack, waiting if necessary if the stack is currently empty- Specified by:
pop
in 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: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 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:
remainingCapacity
in interfaceStack<N>
- Returns:
- int - the number of empty slots available in the stack
-
isEmpty
public final boolean isEmpty() -
clear
public final void clear()clear the stack - does not null old references -
isFull
private boolean isFull()
-