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 AbstractSet
equals, hashCodeMethods inherited from class AbstractCollection
toStringMethods inherited from interface Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface 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:
sizein interfaceCollection- Specified by:
sizein interfaceSet- Specified by:
sizein classAbstractCollection
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection- Specified by:
isEmptyin interfaceSet- Overrides:
isEmptyin classAbstractCollection
-
contains
- Specified by:
containsin interfaceCollection- Specified by:
containsin interfaceSet- Overrides:
containsin classAbstractCollection
-
toArray
- Specified by:
toArrayin interfaceCollection- Specified by:
toArrayin interfaceSet- Overrides:
toArrayin classAbstractCollection
-
toArray
- Specified by:
toArrayin interfaceCollection- Specified by:
toArrayin interfaceSet- Overrides:
toArrayin classAbstractCollection
-
clear
public void clear()- Specified by:
clearin interfaceCollection- Specified by:
clearin interfaceSet- Overrides:
clearin classAbstractCollection
-
iterator
- Specified by:
iteratorin interfaceCollection- Specified by:
iteratorin interfaceIterable- Specified by:
iteratorin interfaceSet- Specified by:
iteratorin classAbstractCollection
-
remove
- Specified by:
removein interfaceCollection- Specified by:
removein interfaceSet- Overrides:
removein classAbstractCollection
-
containsAll
- Specified by:
containsAllin interfaceCollection- Specified by:
containsAllin interfaceSet- Overrides:
containsAllin classAbstractCollection
-
addAll
- Specified by:
addAllin interfaceCollection- Specified by:
addAllin interfaceSet- Overrides:
addAllin classAbstractCollection
-
removeAll
- Specified by:
removeAllin interfaceCollection- Specified by:
removeAllin interfaceSet- Overrides:
removeAllin classAbstractSet
-
retainAll
- Specified by:
retainAllin interfaceCollection- Specified by:
retainAllin interfaceSet- Overrides:
retainAllin classAbstractCollection
-
add
- Specified by:
addin interfaceCollection- Specified by:
addin interfaceSet- Overrides:
addin classAbstractCollection
-