ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CopyOnWriteArraySet.java
Revision: 1.14
Committed: Tue Jan 27 11:36:31 2004 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.13: +5 -0 lines
Log Message:
Add Collection framework membership doc

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain. Use, modify, and
4 * redistribute this code in any way without acknowledgement.
5 */
6
7 package java.util.concurrent;
8 import java.util.*;
9
10 /**
11 * A {@link java.util.Set} that uses {@link
12 * java.util.concurrent.CopyOnWriteArrayList} for all of its
13 * operations. Thus, it shares the same basic properties:
14 * <ul>
15 * <li>It is best suited for applications in which set sizes generally
16 * stay small, read-only operations
17 * vastly outnumber mutative operations, and you need
18 * to prevent interference among threads during traversal.
19 * <li>Mutative operations(add, set, remove, etc) are expensive
20 * since they usually entail copying the entire underlying array.
21 * <li>Iterators do not support the mutative remove operation
22 * <li>Traversal via iterators is very fast and cannot ever encounter
23 * interference from other threads. Iterators rely on
24 * unchanging snapshots of the array at the time the iterators were
25 * constructed.
26 * </ul>
27 * <p>
28 * <b>Sample Usage.</b> Probably the main application
29 * of copy-on-write sets are classes that maintain
30 * sets of Handler objects
31 * that must be multicasted to upon an update command. This
32 * is a classic case where you do not want to be holding a
33 * lock while sending a message, and where traversals normally
34 * vastly overwhelm additions.
35 * <pre>
36 * class Handler { void handle(); ... }
37 *
38 * class X {
39 * private final CopyOnWriteArraySet&lt;Handler&gt; handlers = new CopyOnWriteArraySet&lt;Handler&gt;();
40 * public void addHandler(Handler h) { handlers.add(h); }
41 *
42 * private long internalState;
43 * private synchronized void changeState() { internalState = ...; }
44 *
45 * public void update() {
46 * changeState();
47 * Iterator it = handlers.iterator();
48 * while (it.hasNext())
49 * it.next().handle();
50 * }
51 * }
52 * </pre>
53 * @see CopyOnWriteArrayList
54 *
55 * <p>This class is a member of the
56 * <a href="{@docRoot}/../guide/collections/index.html">
57 * Java Collections Framework</a>.
58 *
59 * @since 1.5
60 * @author Doug Lea
61 * @param <E> the type of elements held in this collection
62 */
63 public class CopyOnWriteArraySet<E> extends AbstractSet<E>
64 implements Cloneable, java.io.Serializable {
65 private static final long serialVersionUID = 5457747651344034263L;
66
67 private final CopyOnWriteArrayList<E> al;
68
69 /**
70 * Constructs an empty set.
71 */
72 public CopyOnWriteArraySet() {
73 al = new CopyOnWriteArrayList<E>();
74 }
75
76 /**
77 * Constructs a set containing all of the elements of the specified
78 * Collection.
79 * @param c the collection
80 */
81 public CopyOnWriteArraySet(Collection<? extends E> c) {
82 al = new CopyOnWriteArrayList<E>();
83 al.addAllAbsent(c);
84 }
85
86
87 public int size() { return al.size(); }
88 public boolean isEmpty() { return al.isEmpty(); }
89 public boolean contains(Object o) { return al.contains(o); }
90 public Object[] toArray() { return al.toArray(); }
91 public <T> T[] toArray(T[] a) { return al.toArray(a); }
92 public void clear() { al.clear(); }
93 public Iterator<E> iterator() { return al.iterator(); }
94 public boolean remove(Object o) { return al.remove(o); }
95 public boolean add(E o) { return al.addIfAbsent(o); }
96 public boolean containsAll(Collection<?> c) { return al.containsAll(c); }
97 public boolean addAll(Collection<? extends E> c) { return al.addAllAbsent(c) > 0; }
98 public boolean removeAll(Collection<?> c) { return al.removeAll(c); }
99 public boolean retainAll(Collection<?> c) { return al.retainAll(c); }
100
101 }