EDU.oswego.cs.dl.util.concurrent
public class CopyOnWriteArraySet extends AbstractSet implements Cloneable, Serializable
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();
}
}
| Field Summary | |
|---|---|
| protected CopyOnWriteArrayList | al |
| Constructor Summary | |
|---|---|
| CopyOnWriteArraySet()
Constructs an empty set | |
| CopyOnWriteArraySet(Collection c)
Constructs a set containing all of the elements of the specified
Collection. | |
| Method Summary | |
|---|---|
| boolean | add(Object o) |
| boolean | addAll(Collection c) |
| void | clear() |
| boolean | contains(Object o) |
| boolean | containsAll(Collection c) |
| boolean | isEmpty() |
| Iterator | iterator() |
| boolean | remove(Object o) |
| boolean | removeAll(Collection c) |
| boolean | retainAll(Collection c) |
| int | size() |
| Object[] | toArray() |
| Object[] | toArray(Object[] a) |