Class ConcurrentList

java.lang.Object
  |
  +--ConcurrentList
All Implemented Interfaces:
java.lang.Cloneable

public class ConcurrentList
extends java.lang.Object
implements java.lang.Cloneable

Implementation of a concurrent list, similar, although not identical, to the List interface. Implements all list operations, and permits all elements (including null) to be stored in the list; however, still not supported operations (especially bulk operations) cause an UnSupportedOperationException. Note that the size of a ConcurrentList has a special semantics: it is defined as the number of iterations, the executing threads needs to traverse the list. In addition to implementing the List interface, the ConcurrentList class provides uniformly named methods to access, insert and remove an element at the beginning and end of the list. These operations allow a concurrent list to be used as a stack, queue, or double-ended queue (deque).

Note that this implementation allows for concurrent operations to take place. Multiple threads may access the same list concurrently, the list need not to be synchronized externally. All available methods including iteration can be performed concurrently, but please note the following special semantics of concurrently manipulating a list.

All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the begining to the end. Additionally to this linear iteration, the indexing thread must compete for locks during the iteration, which possibly makes such operations quite slow.

Semantics of manipulating a list concurrently:

Invariants of the implementation: Notes on the implementation: Possible extensions: Revisions:

Version:
0.4, 08Jul03
Author:
Arno Formella and José B. González López
See Also:
List

Nested Class Summary
private  class ConcurrentList.ListItr
          Private implementation of the ConcurrentIterator.
 
Field Summary
private  ConcurrentItem head
          There are two dummy items which mark the head and the tail of this list.
private  ConcurrentItem tail
          There are two dummy items which mark the head and the tail of this list.
 
Constructor Summary
ConcurrentList()
          Constructs an empty concurrent list.
 
Method Summary
 void add(int index, java.lang.Object element)
          Inserts the specified element at the specified position in this list.
 ConcurrentItem add(java.lang.Object element)
          Appends the given element to the end of this list.
 boolean addAll(java.util.Collection c)
          Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation).
 boolean addAll(int index, java.util.Collection c)
          Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
private  ConcurrentItem addBefore(java.lang.Object element, ConcurrentItem item)
          Basic method to insert an item into this list.
 ConcurrentItem addFirst(java.lang.Object element)
          Inserts the given element at the beginning of this list.
 ConcurrentItem addItem(java.lang.Object element, ConcurrentItem item)
          Inserts the given element to this list before of the given item.
 ConcurrentItem addLast(java.lang.Object element)
          Appends the given element to the end of this list.
 void clear()
          Makes this list the empty list.
protected  java.lang.Object clone()
          Clones this list as currently seen by an iterator constructed for the thread.
 boolean contains(java.lang.Object element)
          Returns true if this list contains the specified element.
 boolean containsAll(java.util.Collection c)
          Returns true if this list contains all of the elements of the specified collection.
 boolean equals(java.lang.Object obj)
          Compares the specified object with this list for equality.
 java.lang.Object get(int index)
          Returns the element at the specified position in this list.
 java.lang.Object getFirst()
          Returns the first element in this list.
 ConcurrentItem getFirstItem()
          Returns the first item in this list.
 ConcurrentItem getItem(int index)
          Returns the item at the specified position in this list.
 ConcurrentItem getItem(int index, ConcurrentItem item)
          Returns the item at the specified position in this list and fills atomicly the parameter item with the element encountered.
(package private)  ConcurrentItem getItem(int index, java.lang.Object[] elementArray)
          Returns the item at the specified position in this list and fills atomicly the array with the element encountered.
 java.lang.Object getLast()
          Returns the last element in this list.
 ConcurrentItem getLastItem()
          Returns the last item in this list.
 int hashCode()
          Returns the hash code value for this list.
 int indexOf(java.lang.Object element)
          Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain the element.
 boolean isEmpty()
          Indicates whether this list is currently empty.
 int lastIndexOf(java.lang.Object element)
          Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
 ConcurrentIterator listIterator()
          Returns a ConcurrentIterator of the items or elements in this list (in proper sequence), starting at the beginning of this list.
 ConcurrentIterator listIterator(int index)
          Returns a ConcurrentIterator of the items or elements in this list (in proper sequence), starting at the specified position in the list.
private  void remove(ConcurrentItem item)
          Basic method to remove an item from this list.
 java.lang.Object remove(int index)
          Removes the element at the specified position in this list.
 boolean remove(java.lang.Object element)
          Removes the first occurrence of the specified element in this list.
 boolean removeAll(java.util.Collection c)
          Removes from this list all the elements that are contained in the specified collection (optional operation).
 java.lang.Object removeFirst()
          Removes and returns the first element from this list.
 void removeItem(ConcurrentItem item)
          Removes an item from this list.
 java.lang.Object removeLast()
          Removes and returns the last element from this list.
 boolean retainAll(java.util.Collection c)
          Retains only the elements in this list that are contained in the specified collection (optional operation).
 java.lang.Object set(int index, java.lang.Object element)
          Sets the element at the specified position.
 int size()
          The size of a ConcurrentList is defined as the number of iterations currently needed by this thread to traverse this list.
 ConcurrentList subList(int fromIndex, int toIndex)
          Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
 java.lang.Object[] toArray()
          Returns an array containing all elements of this list as seen currently by an iteration of the thread over this list.
(package private)  java.lang.Object[] toArray(java.lang.Object[] a)
          Returns an array containing all elements of this list as seen currently by an iteration of the thread over this list.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

head

private transient volatile ConcurrentItem head
There are two dummy items which mark the head and the tail of this list. A user of this list will never have access to these items. Two markers are needed to guarantee deadlock free access in all operations: locks on items are always aquired in strict order in direction from head to tail.


tail

private transient volatile ConcurrentItem tail
There are two dummy items which mark the head and the tail of this list. A user of this list will never have access to these items. Two markers are needed to guarantee deadlock free access in all operations: locks on items are always aquired in strict order in direction from head to tail.

Constructor Detail

ConcurrentList

public ConcurrentList()
Constructs an empty concurrent list.

Method Detail

isEmpty

public boolean isEmpty()
Indicates whether this list is currently empty.

Returns:
true if list is currently empty.

getFirst

public java.lang.Object getFirst()
Returns the first element in this list.

Returns:
the first element in this list.
Throws:
java.util.NoSuchElementException - if this list is currently empty.

getLast

public java.lang.Object getLast()
Returns the last element in this list.

Returns:
the last element in this list.
Throws:
java.util.NoSuchElementException - if this list is currently empty.

getFirstItem

public ConcurrentItem getFirstItem()
Returns the first item in this list.

Returns:
the first item in this list or null if this list is currently empty.

getLastItem

public ConcurrentItem getLastItem()
Returns the last item in this list.

Returns:
the last item in this list or null if this list is currently empty.

removeFirst

public java.lang.Object removeFirst()
                             throws java.lang.InterruptedException
Removes and returns the first element from this list.

Returns:
the first element from this list.
Throws:
java.util.NoSuchElementException - if this list is currently empty.
java.lang.InterruptedException - if the thread is interrupted while waiting for iterators releasing the first item.

removeLast

public java.lang.Object removeLast()
                            throws java.lang.InterruptedException
Removes and returns the last element from this list.

Returns:
the last element from this list.
Throws:
java.util.NoSuchElementException - if this list is currently empty.
java.lang.InterruptedException - if the thread is interrupted while waiting for iterators releasing the last item.

removeItem

public void removeItem(ConcurrentItem item)
                throws java.lang.InterruptedException
Removes an item from this list.

Parameters:
item - the item to be removed. Precondition: item is part of this list.
Throws:
java.util.NoSuchElementException - of the this list is currently empty.
java.lang.InterruptedException - if the thread is interrupted while waiting for iterators releasing the last item.

addFirst

public ConcurrentItem addFirst(java.lang.Object element)
Inserts the given element at the beginning of this list.

Parameters:
element - the element to be inserted at the beginning of this list.
Returns:
the item that has been inserted.

addLast

public ConcurrentItem addLast(java.lang.Object element)
Appends the given element to the end of this list.

Parameters:
element - the element to be inserted at the end of this list.
Returns:
the item that has been inserted.

add

public ConcurrentItem add(java.lang.Object element)
Appends the given element to the end of this list.

Parameters:
element - the element to be inserted at the end of this list.
Returns:
always true.

addItem

public ConcurrentItem addItem(java.lang.Object element,
                              ConcurrentItem item)
Inserts the given element to this list before of the given item.

Parameters:
element - the element to be inserted.
item - the item which the element is inserted before. Precondition: item is part of this list.
Throws:
java.util.NoSuchElementException - if the item has been deleted while waiting.

clear

public void clear()
           throws java.lang.InterruptedException
Makes this list the empty list.

java.lang.InterruptedException

listIterator

public ConcurrentIterator listIterator()
Returns a ConcurrentIterator of the items or elements in this list (in proper sequence), starting at the beginning of this list. The iterator is fail-save: if this list is structurally modified at any time after the iterator is created, the iterator will continue to the end of this list. Modifications behind the current position of the iterator (i.e. in direction to the beginning) will not be reflected during this iteration, modifications in front of the current position of the iterator (i.e. in direction of the ending) will be included in the on-going iteration.

Note that you must use the removeCurrent method of the iterator to remove the current item while iterating. It is a severe bug to call the remove method of the list instead, because it will deadlock. The implementation can neither detect nor prevent this deadlock.

Returns:
an Iterator of the elements in this list (in proper sequence), starting at the beginning of this list.
See Also:
Iterator

listIterator

public ConcurrentIterator listIterator(int index)
Returns a ConcurrentIterator of the items or elements in this list (in proper sequence), starting at the specified position in the list. The iterator is fail-save: if this list is structurally modified at any time after the iterator is created, the iterator will continue to the end of this list. Modifications behind the current position of the iterator (i.e. in direction to the beginning) will not be reflected during this iteration, modifications in front of the current position of the iterator (i.e. in direction of the ending) will be included in the on-going iteration.

Note that you must use the removeCurrent() method of the iterator to remove the current item while iterating. It is a severe bug to call the remove() method of the list instead, because it will deadlock. The implementation can neither detect nor prevent this deadlock.

Parameters:
index - index of first element to be returned from the iterator (by a call to next()).
Returns:
a ConcurrentIterator of the items or elements in this list (in proper sequence), starting at the specified position in the list.
Throws:
java.lang.IndexOutOfBoundsException - if index is out of range.

addBefore

private ConcurrentItem addBefore(java.lang.Object element,
                                 ConcurrentItem item)
Basic method to insert an item into this list.

Parameters:
element - the element to be inserted
item - the item in front of which the element is to be inserted. Precondition: item is part of the list.
Returns:
the item that has been inserted.
Throws:
java.util.NoSuchElementException - if the item in front of which the element has to be inserted has been removed from this list while the thread is waiting for the necessary locks.

remove

private void remove(ConcurrentItem item)
             throws java.lang.InterruptedException
Basic method to remove an item from this list.

Parameters:
item - the item to be removed. Precondition: item is part of this list.
Throws:
java.lang.InterruptedException - if the thread is interrupted while waiting for iterators releasing the item.
java.util.NoSuchElementException - if this list is empty.

clone

protected java.lang.Object clone()
                          throws java.lang.CloneNotSupportedException
Clones this list as currently seen by an iterator constructed for the thread. Note that due to the concurrent nature of a ConcurrentList the resulting clone might be a list that never existed exactly like this during the execution of the program. However, if the list is not modified during cloning, the clone is an exact copy.

Overrides:
clone in class java.lang.Object
Returns:
A new ConcurrentList containing all elements as seen by the executing thread during the copy iteration.
java.lang.CloneNotSupportedException

get

public java.lang.Object get(int index)
Returns the element at the specified position in this list. Note that the method iterates from the beginning of this list, hence the operation might be quite slow.

Parameters:
index - the index of the element to be returned.
Returns:
the object at the position specified by index.
Throws:
java.lang.IndexOutOfBoundsException - if the specified index is out of range.

getItem

public ConcurrentItem getItem(int index)
Returns the item at the specified position in this list. Note that the method iterates from the beginning of this list, hence the operation might be quite slow.

Parameters:
index - the index of the item to be returned.
Returns:
the item at the position specified by index or null if no such item exists.

getItem

ConcurrentItem getItem(int index,
                       java.lang.Object[] elementArray)
Returns the item at the specified position in this list and fills atomicly the array with the element encountered. The method is only visible in the package, but there is a wrapper method which uses an item to transfer the element to the caller. The design with an array is easy to extent if more than one element should be returned. Note that the method iterates from the beginning of this list, hence the operation might be quite slow.

Parameters:
index - the index of the item to be returned.
Returns:
the item at the position specified by index or null if no such item exists.

getItem

public ConcurrentItem getItem(int index,
                              ConcurrentItem item)
Returns the item at the specified position in this list and fills atomicly the parameter item with the element encountered. Allows for an atomic access to an item of the list. If one uses the separate forms of first accessing the item then accessing the element, another thread might have changed the element of the item in the mean while. Note that the method iterates from the beginning of this list, hence the operation might be quite slow.

Parameters:
index - the index of the item to be returned.
Returns:
the item at the position specified by index or null if no such item exists.

set

public java.lang.Object set(int index,
                            java.lang.Object element)
Sets the element at the specified position. Note that the method iterates from the beginning of this list, hence the operation might be quite slow.

Parameters:
index - the index of the item to be returned.
element - the element used to replace
Returns:
the element found at the position specified by index.
Throws:
java.lang.IndexOutOfBoundsException - if the specified index is out of range.

contains

public boolean contains(java.lang.Object element)
Returns true if this list contains the specified element. Note that the method iterates from the beginning of this list, hence the operation might be quite slow. More formally, returns true if and only if this list contains at least one element e such that (element==null ? e==null : element.equals(e)).

Parameters:
element - element whose presence in this list is to be tested.
Returns:
true if this list contains the specified element.

indexOf

public int indexOf(java.lang.Object element)
Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (element==null ? get(i)==null : element.equals(get(i))), or -1 if there is no such index.

Parameters:
element - element to search for.
Returns:
the index in this list of the first occurrence of the specified element, or -1 if the list does not contain the element.

add

public void add(int index,
                java.lang.Object element)
Inserts the specified element at the specified position in this list. Hence, shifts the element currently at that position (if any) and any subsequent elements to the right.

Parameters:
index - index at which the specified element is to be inserted.
element - element to be inserted.
Throws:
java.lang.IndexOutOfBoundsException - if the specified index is out of range.

remove

public java.lang.Object remove(int index)
                        throws java.lang.InterruptedException
Removes the element at the specified position in this list. hence, shifts any subsequent elements to the left. Returns the element that was removed from the list.

Parameters:
index - the index of the element to removed.
Returns:
the element previously at the specified position.
Throws:
java.lang.IndexOutOfBoundsException - if the specified index is out of range.
java.lang.InterruptedException - if the thread is interrupted while waiting for iterators releasing the item

remove

public boolean remove(java.lang.Object element)
               throws java.lang.InterruptedException
Removes the first occurrence of the specified element in this list. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (element==null ? get(i)==null : element.equals(get(i))) (if such an element exists).

Parameters:
element - element to be removed from this list, if present.
Returns:
true if the list contained the specified element.
Throws:
java.lang.InterruptedException - if the thread is interrupted while waiting for iterators releasing the item holding the element.

addAll

public boolean addAll(java.util.Collection c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

Parameters:
c - collection whose elements are to be added to this list.
Returns:
true if this list changed as a result of the call.
Throws:
java.lang.UnsupportedOperationException - if the addAll() method is not supported by this list.
java.lang.ClassCastException - if the class of an element in the specified collection prevents it from being added to this list.
java.lang.IllegalArgumentException - if some aspect of an element in the specified collection prevents it from being added to this list.
See Also:
add(Object)

addAll

public boolean addAll(int index,
                      java.util.Collection c)
Inserts all of the elements in the specified collection into this list at the specified position (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that they are returned by the specified collection's iterator. The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

Parameters:
index - index at which to insert first element from the specified collection.
c - elements to be inserted into this list.
Returns:
true if this list changed as a result of the call.
Throws:
java.lang.UnsupportedOperationException - if the addAll() method is not supported by this list.
java.lang.ClassCastException - if the class of one of elements of the specified collection prevents it from being added to this list.
java.lang.IllegalArgumentException - if some aspect of one of elements of the specified collection prevents it from being added to this list.
java.lang.IndexOutOfBoundsException - if the index is out of range.

containsAll

public boolean containsAll(java.util.Collection c)
Returns true if this list contains all of the elements of the specified collection.

Parameters:
c - collection to be checked for containment in this list.
Returns:
true if this list contains all of the elements of the specified collection.
See Also:
contains(Object)

removeAll

public boolean removeAll(java.util.Collection c)
Removes from this list all the elements that are contained in the specified collection (optional operation).

Parameters:
c - collection that defines which elements will be removed from this list.
Returns:
true if this list changed as a result of the call.
Throws:
java.lang.UnsupportedOperationException - if the removeAll() method is not supported by this list.
See Also:
remove(Object), contains(Object)

retainAll

public boolean retainAll(java.util.Collection c)
Retains only the elements in this list that are contained in the specified collection (optional operation). In other words, removes from this list all the elements that are not contained in the specified collection.

Parameters:
c - collection that defines which elements this set will retain.
Returns:
true if this list changed as a result of the call.
Throws:
java.lang.UnsupportedOperationException - if the retainAll() method is not supported by this list.
See Also:
remove(Object), contains(Object)

equals

public boolean equals(java.lang.Object obj)
Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

Overrides:
equals in class java.lang.Object
Parameters:
obj - the object to be compared for equality with this list.
Returns:
true if the specified object is equal to this list.
Throws:
java.lang.UnsupportedOperationException - if the retainAll() method is not supported by this list.

hashCode

public int hashCode()
Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:
       hashCode = 1;
       Iterator i = list.iterator();
       while (i.hasNext()) {
         Object obj = i.next();
         hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
      }
     
This ensures that list1.equals(list2) implies that list1.hashCode()==list2.hashCode() for any two lists, list1 and list2, as required by the general contract of Object.hashCode.

Overrides:
hashCode in class java.lang.Object
Returns:
the hash code value for this list.
Throws:
java.lang.UnsupportedOperationException - if the retainAll() method is not supported by this list.
See Also:
Object.hashCode(), Object.equals(Object), equals(Object)

lastIndexOf

public int lastIndexOf(java.lang.Object element)
Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element. More formally, returns the highest index i such that (element==null ? get(i)==null : element.equals(get(i))), or -1 if there is no such index.

Parameters:
element - element to search for.
Returns:
the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
Throws:
java.lang.UnsupportedOperationException - if the retainAll() method is not supported by this list.

size

public int size()
The size of a ConcurrentList is defined as the number of iterations currently needed by this thread to traverse this list. Note that the information returned by this method is not reliable if other threads are concurrently manipulating this list. Hence, subsequent accesses to the list might see a different number of elements. Note that the operation might be quite slow, because the number of elements must be counted on the fly.

Returns:
the size of this list as currently seen by the thread.

toArray

public java.lang.Object[] toArray()
Returns an array containing all elements of this list as seen currently by an iteration of the thread over this list. Note that due to the concurrent nature of a ConcurrentList the resulting array might be view of a list that never existed exactly like this during the execution of the program. However, if the list is not modified during the exection of this method, the array is an exact copy.

Returns:
an array containing all elements of this list.

toArray

java.lang.Object[] toArray(java.lang.Object[] a)
Returns an array containing all elements of this list as seen currently by an iteration of the thread over this list. Note that due to the concurrent nature of a ConcurrentList the resulting array might be a view of the list that never existed exactly like this during the execution of the program. However, if the list is not modified during the exection of this method, the array is an exact copy.

Parameters:
a - the array to hold the elements of this list.
Returns:
an array containing all elements of this list.
Throws:
java.lang.IndexOutOfBoundsException - if the array is not sufficiently large to hold all elements

subList

public ConcurrentList subList(int fromIndex,
                              int toIndex)
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.

This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:

         list.subList(from, to).clear();
     
Similar idioms may be constructed for indexOf and lastIndexOf, and all of the algorithms in the Collections class can be applied to a subList.

The semantics of this list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)

Parameters:
fromIndex - low endpoint (inclusive) of the subList.
toIndex - high endpoint (exclusive) of the subList.
Returns:
a view of the specified range within this list.
Throws:
java.lang.IndexOutOfBoundsException - for an illegal endpoint index value (fromIndex < 0 || toIndex > size || fromIndex > toIndex).
java.lang.UnsupportedOperationException - if the method is not supported by this list.