Package EDU.oswego.cs.dl.util.concurrent
Class CopyOnWriteArraySet
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet
-
- EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArraySet
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,java.lang.Iterable,java.util.Collection,java.util.Set
public class CopyOnWriteArraySet extends java.util.AbstractSet implements java.lang.Cloneable, java.io.SerializableThis class implements a java.util.Set that uses a CopyOnWriteArrayList for all of its operations. Thus, it shares the same basic properties:- It is best suited for applications in which set sizes generally stay small, read-only operations vastly outnumber mutative operations, and you need to prevent interference among threads during traversal.
- Mutative operations(add, set, remove, etc) are fairly expensive since they usually entail copying the entire underlying array.
- Loops involving repeated element-by-element mutative operations are so expensive that they should generally be avoided.
- Iterators do not support the mutative remove operation
- Traversal via iterators is very fast and cannot ever encounter interference from other threads. Iterators rely on unchanging snapshots of the array at the time the iterators were constructed
Sample Usage. Probably the main application of copy-on-write sets are classes that maintain sets of Handler objects that must be multicasted to upon an update command. This is a classic case where you do not want to be holding a synch lock while sending a message, and where traversals normally vastly overwhelm additions.
class Handler { void handle(); ... } class X { private final CopyOnWriteArraySet handlers = new CopyOnWriteArraySet(); public void addHandler(Handler h) { handlers.add(h); } private long internalState; private synchronized void changeState() { internalState = ...; } public void update() { changeState(); Iterator it = handlers.iterator(); while (it.hasNext()) ((Handler)(it.next()).handle(); } }- See Also:
CopyOnWriteArrayList, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected CopyOnWriteArrayListal
-
Constructor Summary
Constructors Constructor Description CopyOnWriteArraySet()Constructs an empty setCopyOnWriteArraySet(java.util.Collection c)Constructs a set containing all of the elements of the specified Collection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(java.lang.Object o)booleanaddAll(java.util.Collection c)voidclear()booleancontains(java.lang.Object o)booleancontainsAll(java.util.Collection c)booleanisEmpty()java.util.Iteratoriterator()booleanremove(java.lang.Object o)booleanremoveAll(java.util.Collection c)booleanretainAll(java.util.Collection c)intsize()java.lang.Object[]toArray()java.lang.Object[]toArray(java.lang.Object[] a)
-
-
-
Field Detail
-
al
protected final CopyOnWriteArrayList al
-
-
Method Detail
-
size
public int size()
- Specified by:
sizein interfacejava.util.Collection- Specified by:
sizein interfacejava.util.Set- Specified by:
sizein classjava.util.AbstractCollection
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfacejava.util.Collection- Specified by:
isEmptyin interfacejava.util.Set- Overrides:
isEmptyin classjava.util.AbstractCollection
-
contains
public boolean contains(java.lang.Object o)
- Specified by:
containsin interfacejava.util.Collection- Specified by:
containsin interfacejava.util.Set- Overrides:
containsin classjava.util.AbstractCollection
-
toArray
public java.lang.Object[] toArray()
- Specified by:
toArrayin interfacejava.util.Collection- Specified by:
toArrayin interfacejava.util.Set- Overrides:
toArrayin classjava.util.AbstractCollection
-
toArray
public java.lang.Object[] toArray(java.lang.Object[] a)
- Specified by:
toArrayin interfacejava.util.Collection- Specified by:
toArrayin interfacejava.util.Set- Overrides:
toArrayin classjava.util.AbstractCollection
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Collection- Specified by:
clearin interfacejava.util.Set- Overrides:
clearin classjava.util.AbstractCollection
-
iterator
public java.util.Iterator iterator()
- Specified by:
iteratorin interfacejava.util.Collection- Specified by:
iteratorin interfacejava.lang.Iterable- Specified by:
iteratorin interfacejava.util.Set- Specified by:
iteratorin classjava.util.AbstractCollection
-
remove
public boolean remove(java.lang.Object o)
- Specified by:
removein interfacejava.util.Collection- Specified by:
removein interfacejava.util.Set- Overrides:
removein classjava.util.AbstractCollection
-
containsAll
public boolean containsAll(java.util.Collection c)
- Specified by:
containsAllin interfacejava.util.Collection- Specified by:
containsAllin interfacejava.util.Set- Overrides:
containsAllin classjava.util.AbstractCollection
-
addAll
public boolean addAll(java.util.Collection c)
- Specified by:
addAllin interfacejava.util.Collection- Specified by:
addAllin interfacejava.util.Set- Overrides:
addAllin classjava.util.AbstractCollection
-
removeAll
public boolean removeAll(java.util.Collection c)
- Specified by:
removeAllin interfacejava.util.Collection- Specified by:
removeAllin interfacejava.util.Set- Overrides:
removeAllin classjava.util.AbstractSet
-
retainAll
public boolean retainAll(java.util.Collection c)
- Specified by:
retainAllin interfacejava.util.Collection- Specified by:
retainAllin interfacejava.util.Set- Overrides:
retainAllin classjava.util.AbstractCollection
-
add
public boolean add(java.lang.Object o)
- Specified by:
addin interfacejava.util.Collection- Specified by:
addin interfacejava.util.Set- Overrides:
addin classjava.util.AbstractCollection
-
-