E
- the type of elements held in this collection@Beta @ThreadSafe public final class ConcurrentLinkedStack<E> extends AbstractCollection<E> implements Serializable
ConcurrentLinkedStack
is an appropriate choice when many threads will exchange elements
through shared access to a common collection. Like most other concurrent collection
implementations, this class does not permit the use of null
elements.
This implementation employs elimination and combination to transfer elements between threads that are pushing and popping concurrently. This technique avoids contention on the stack by attempting to cancel opposing operations and merge additive operations if an immediate update to the stack is not successful. When a pair of additive operations collide, the task of performing the combined set of operations is delegated to one of the threads and the other thread optionally waits for its operation to be completed. This decision of whether to wait for completion is determined by constructing either a linearizable or optimistic stack.
Iterators are weakly consistent, returning elements reflecting the state of the stack at
some point at or since the creation of the iterator. They do not throw
ConcurrentModificationException
, and may proceed concurrently with other
operations. Elements contained in the stack since the creation of the iterator will be returned
exactly once.
Beware that, unlike in most collections, the size
method is NOT a constant-time
operation. Because of the asynchronous nature of these stacks, determining the current number of
elements requires a traversal of the elements, and so may report inaccurate results if this
collection is modified during traversal.
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e) |
boolean |
addAll(Collection<? extends E> c) |
Queue<E> |
asLifoQueue()
Returns a view as a last-in-first-out (LIFO)
Queue . |
void |
clear() |
boolean |
contains(Object o) |
boolean |
isEmpty() |
Iterator<E> |
iterator() |
static <E> ConcurrentLinkedStack<E> |
linearizable()
Creates a stack with a linearizable backoff strategy.
|
static <E> ConcurrentLinkedStack<E> |
optimistic()
Creates a stack with an optimistic backoff strategy.
|
E |
peek()
Retrieves, but does not remove, the top of the stack (in other words, the last element pushed),
or returns
null if this stack is empty. |
E |
pop()
Removes and returns the top element or returns
null if this stack is empty. |
void |
push(E e)
Pushes an element onto the stack (in other words, adds an element at the top of this stack).
|
boolean |
remove(Object o) |
int |
size()
Returns the number of elements in this stack.
|
containsAll, removeAll, retainAll, toArray, toArray, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
equals, hashCode, parallelStream, removeIf, spliterator, stream
public static <E> ConcurrentLinkedStack<E> optimistic()
E
- the type of elements held in this collectionpublic static <E> ConcurrentLinkedStack<E> linearizable()
E
- the type of elements held in this collectionpublic boolean isEmpty()
isEmpty
in interface Collection<E>
isEmpty
in class AbstractCollection<E>
public int size()
Beware that, unlike in most collections, this method is NOT a constant-time operation. Because of the asynchronous nature of these stacks, determining the current number of elements requires an O(n) traversal. Additionally, if elements are added or removed during execution of this method, the returned result may be inaccurate. Thus, this method is typically not very useful in concurrent applications.
size
in interface Collection<E>
size
in class AbstractCollection<E>
public void clear()
clear
in interface Collection<E>
clear
in class AbstractCollection<E>
public boolean contains(@Nullable Object o)
contains
in interface Collection<E>
contains
in class AbstractCollection<E>
@Nullable public E peek()
null
if this stack is empty.null
if this stack is empty@Nullable public E pop()
null
if this stack is empty.null
if this stack is emptypublic void push(@Nonnull E e)
e
- the element to pushpublic boolean add(E e)
add
in interface Collection<E>
add
in class AbstractCollection<E>
public boolean addAll(Collection<? extends E> c)
addAll
in interface Collection<E>
addAll
in class AbstractCollection<E>
public boolean remove(Object o)
remove
in interface Collection<E>
remove
in class AbstractCollection<E>
public Iterator<E> iterator()
iterator
in interface Iterable<E>
iterator
in interface Collection<E>
iterator
in class AbstractCollection<E>