Package EDU.oswego.cs.dl.util.concurrent
Class CopyOnWriteArraySet
java.lang.Object
java.util.AbstractCollection
java.util.AbstractSet
EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArraySet
- All Implemented Interfaces:
Serializable
,Cloneable
,Iterable
,Collection
,Set
This 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:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty setConstructs a set containing all of the elements of the specified Collection. -
Method Summary
Methods inherited from class java.util.AbstractSet
equals, hashCode
Methods inherited from class java.util.AbstractCollection
toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.Set
spliterator
-
Field Details
-
al
-
-
Constructor Details
-
CopyOnWriteArraySet
public CopyOnWriteArraySet()Constructs an empty set -
CopyOnWriteArraySet
Constructs a set containing all of the elements of the specified Collection.
-
-
Method Details
-
size
public int size()- Specified by:
size
in interfaceCollection
- Specified by:
size
in interfaceSet
- Specified by:
size
in classAbstractCollection
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection
- Specified by:
isEmpty
in interfaceSet
- Overrides:
isEmpty
in classAbstractCollection
-
contains
- Specified by:
contains
in interfaceCollection
- Specified by:
contains
in interfaceSet
- Overrides:
contains
in classAbstractCollection
-
toArray
- Specified by:
toArray
in interfaceCollection
- Specified by:
toArray
in interfaceSet
- Overrides:
toArray
in classAbstractCollection
-
toArray
- Specified by:
toArray
in interfaceCollection
- Specified by:
toArray
in interfaceSet
- Overrides:
toArray
in classAbstractCollection
-
clear
public void clear()- Specified by:
clear
in interfaceCollection
- Specified by:
clear
in interfaceSet
- Overrides:
clear
in classAbstractCollection
-
iterator
- Specified by:
iterator
in interfaceCollection
- Specified by:
iterator
in interfaceIterable
- Specified by:
iterator
in interfaceSet
- Specified by:
iterator
in classAbstractCollection
-
remove
- Specified by:
remove
in interfaceCollection
- Specified by:
remove
in interfaceSet
- Overrides:
remove
in classAbstractCollection
-
containsAll
- Specified by:
containsAll
in interfaceCollection
- Specified by:
containsAll
in interfaceSet
- Overrides:
containsAll
in classAbstractCollection
-
addAll
- Specified by:
addAll
in interfaceCollection
- Specified by:
addAll
in interfaceSet
- Overrides:
addAll
in classAbstractCollection
-
removeAll
- Specified by:
removeAll
in interfaceCollection
- Specified by:
removeAll
in interfaceSet
- Overrides:
removeAll
in classAbstractSet
-
retainAll
- Specified by:
retainAll
in interfaceCollection
- Specified by:
retainAll
in interfaceSet
- Overrides:
retainAll
in classAbstractCollection
-
add
- Specified by:
add
in interfaceCollection
- Specified by:
add
in interfaceSet
- Overrides:
add
in classAbstractCollection
-