|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.util.AbstractCollection<E>
java.util.AbstractQueue<E>
org.amino.ds.lockfree.LockFreeDeque<E>
E - type of element in the dequepublic class LockFreeDeque<E>
This Deque implementation is based on the algorithm defined in the follwoing paper: CAS-Based Lock-Free Algorithm for Shared Deques By Maged M. Michael
As the terminology between below description differs from the Deque interface defined in Java 6, please translate left = last and right = first. Right => First, Left => Last
The deque is a double ended queue. Insertion and deletion can happen at both ends. The basic deque is maintained as a doubly linked list. An anchor data structure which comprise of 2 pointers (left and right) is used to keep track of the leftmost and rightmost element of the deque. A deque status flag is maintained to keep track of the state of the deque.
The deque is said to be in a stable state when it is 1) empty, 2) contains a single item and both the anchor pointers point to the item and 3) for all nodes x in the deque, x.right.left = x unless x is the rightmost node and x.left.right = x unless x is the leftmost node.
There are 4 unstable states for the deque. A deque can be unstable only if it has 2 or more elements. Also, only one end of the deque can have problems at any given time and the status has to show that it is unstable with the flag having a value of RPUSH or LPUSH depending on the problem side (right or left). Unstable states: 1) x.left.right != x, where x is the rightmost node. Deque status flag = RPUSH 2) x.right.left != x, where x is the leftmost node. Deque status flag = LPUSH It is acceptable for a deque to have no problems but the status flag may show that it is in an unstable state. So, the deque may have a status of RPUSH or LPUSH but still be coherent (the other two unstable states)
Insertion or deletion from either end can only be done on a stable deque. If the deque is in an unstable state it has to be stabilized first before the operation can proceed. After a right insertion the deque status is changed to an RPUSH atomically with the insertion. Similarly after a left insertion the deque status is changed to LPUSH atomically with the insertion. To put the deque back to a stable state the pointers have to be made coherent.
Thread Safety:
Follow methods are NOT thread-safe:
| Constructor Summary | |
|---|---|
LockFreeDeque()
|
|
| Method Summary | |
|---|---|
void |
addFirst(E d)
This is the method that does a right push into the Deque. |
void |
addLast(E d)
This is the method that does a left push into the Deque. |
void |
clear()
Removes all elements, clears the deque. |
boolean |
contains(java.lang.Object o)
Contains function is not thread safe. |
java.util.Iterator<E> |
descendingIterator()
|
E |
getFirst()
|
E |
getLast()
|
boolean |
isEmpty()
Checks to see if the deque is empty or not. |
java.util.Iterator<E> |
iterator()
|
boolean |
offer(E e)
Inserts element e at the tail of this deque. |
boolean |
offerFirst(E e)
|
boolean |
offerLast(E e)
|
E |
peek()
Returns the first element of this deque, or null if deque is empty. |
E |
peekFirst()
|
E |
peekLast()
|
E |
poll()
Returns and removes first (head) element of this deque, or null if this deque is empty. |
E |
pollFirst()
This is the method to pop the Right node from the Deque. |
E |
pollLast()
|
E |
pop()
|
void |
push(E e)
|
E |
remove()
Returns and removes the first element in this deque. |
boolean |
remove(java.lang.Object o)
Remove first occurrence of specified object o in this deque. |
E |
removeFirst()
|
boolean |
removeFirstOccurrence(java.lang.Object o)
|
E |
removeLast()
|
boolean |
removeLastOccurrence(java.lang.Object o)
|
int |
size()
|
| Methods inherited from class java.util.AbstractQueue |
|---|
add, addAll, element |
| Methods inherited from class java.util.AbstractCollection |
|---|
containsAll, removeAll, retainAll, toArray, toArray, toString |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface java.util.Deque |
|---|
add, element |
| Methods inherited from interface java.util.Collection |
|---|
addAll, containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray |
| Constructor Detail |
|---|
public LockFreeDeque()
| Method Detail |
|---|
public boolean offer(E e)
addLast(E).
offer in interface java.util.Deque<E>offer in interface java.util.Queue<E>e - element to insert
public E peek()
peek in interface java.util.Deque<E>peek in interface java.util.Queue<E>public E poll()
poll in interface java.util.Deque<E>poll in interface java.util.Queue<E>public E remove()
remove in interface java.util.Deque<E>remove in interface java.util.Queue<E>remove in class java.util.AbstractQueue<E>public java.util.Iterator<E> descendingIterator()
descendingIterator in interface java.util.Deque<E>public E getFirst()
getFirst in interface java.util.Deque<E>public E getLast()
getLast in interface java.util.Deque<E>public boolean offerFirst(E e)
offerFirst in interface java.util.Deque<E>public boolean offerLast(E e)
offerLast in interface java.util.Deque<E>public E removeFirst()
removeFirst in interface java.util.Deque<E>public boolean removeFirstOccurrence(java.lang.Object o)
removeFirstOccurrence in interface java.util.Deque<E>public E removeLast()
removeLast in interface java.util.Deque<E>public boolean removeLastOccurrence(java.lang.Object o)
removeLastOccurrence in interface java.util.Deque<E>public void clear()
clear in interface java.util.Collection<E>clear in class java.util.AbstractQueue<E>public boolean contains(java.lang.Object o)
contains in interface java.util.Collection<E>contains in interface java.util.Deque<E>contains in class java.util.AbstractCollection<E>o - is the Object we are looking for.
java.lang.UnsupportedOperationExceptionpublic boolean isEmpty()
isEmpty in interface java.util.Collection<E>isEmpty in class java.util.AbstractCollection<E>public java.util.Iterator<E> iterator()
iterator in interface java.lang.Iterable<E>iterator in interface java.util.Collection<E>iterator in interface java.util.Deque<E>iterator in class java.util.AbstractCollection<E>public boolean remove(java.lang.Object o)
remove in interface java.util.Collection<E>remove in interface java.util.Deque<E>remove in class java.util.AbstractCollection<E>o - element to be removed, if present.
public int size()
size in interface java.util.Collection<E>size in interface java.util.Deque<E>size in class java.util.AbstractCollection<E>public void addFirst(E d)
addFirst in interface java.util.Deque<E>d - element being addedpublic E peekFirst()
peekFirst in interface java.util.Deque<E>public E pollFirst()
pollFirst in interface java.util.Deque<E>public void addLast(E d)
addLast in interface java.util.Deque<E>d - element to addpublic E peekLast()
peekLast in interface java.util.Deque<E>public E pollLast()
pollLast in interface java.util.Deque<E>public E pop()
pop in interface java.util.Deque<E>public void push(E e)
push in interface java.util.Deque<E>e - element pushed
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||