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 java.lang.Object implements BlockingStack<N>
Concurrent "lock-free" version of a stack.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
ConcurrentStack.SpinningStackNotEmpty
private class
ConcurrentStack.SpinningStackNotFull
private class
ConcurrentStack.StackNotEmpty
private class
ConcurrentStack.StackNotFull
private class
ConcurrentStack.WaitingStackNotEmpty
private class
ConcurrentStack.WaitingStackNotFull
-
Field Summary
Fields Modifier and Type Field Description private SequenceLock
seqLock
private int
size
private java.util.concurrent.atomic.AtomicReferenceArray<N>
stack
private Condition
stackNotEmptyCondition
private Condition
stackNotFullCondition
private ContendedAtomicInteger
stackTop
-
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 void
clear()
clear the stack - does not null old referencesboolean
contains(N n)
Linear search the stack for contains - not an efficient operationboolean
isEmpty()
private boolean
isFull()
N
peek()
peek at the top of the stackN
pop()
pop the next element off the stackN
pop(long time, java.util.concurrent.TimeUnit unit)
Pop an element from the stack, waiting if necessary if the stack is currently emptyN
popInterruptibly()
Pop an element from the stack, waiting as long as required for an element to become available on the stackboolean
push(N n)
add an element to the stack, failing if the stack is unable to growboolean
push(N n, long time, java.util.concurrent.TimeUnit unit)
Push an element on the stack, waiting if necessary if the stack is currently fullvoid
pushInterruptibly(N n)
Push an element on the stack waiting as long as required for space to become availableint
remainingCapacity()
how much available space in the stackint
size()
Return the size of the stack
-
-
-
Field Detail
-
size
private final int size
-
stack
private final java.util.concurrent.atomic.AtomicReferenceArray<N> stack
-
stackTop
private final ContendedAtomicInteger stackTop
-
seqLock
private final SequenceLock seqLock
-
stackNotFullCondition
private final Condition stackNotFullCondition
-
stackNotEmptyCondition
private final Condition stackNotEmptyCondition
-
-
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, java.util.concurrent.TimeUnit unit) throws java.lang.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 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:
java.lang.InterruptedException
- on interrupt
-
pushInterruptibly
public final void pushInterruptibly(N n) throws java.lang.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 interfaceBlockingStack<N>
- Parameters:
n
- - the element to push- Throws:
java.lang.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
-
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, java.util.concurrent.TimeUnit unit) throws java.lang.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 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:
java.lang.InterruptedException
- on interrupt
-
popInterruptibly
public final N popInterruptibly() throws java.lang.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 interfaceBlockingStack<N>
- Returns:
- N - the popped element
- Throws:
java.lang.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()
-
-