ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CopyOnWriteArraySet.java
Revision: 1.1
Committed: Wed May 14 21:30:46 2003 UTC (21 years ago) by tim
Branch: MAIN
Log Message:
Moved main source rooted at . to ./src/main
Moved test source rooted at ./etc/testcases to ./src/test

File Contents

# User Rev Content
1 tim 1.1 package java.util.concurrent;
2    
3     import java.util.*;
4    
5     /**
6     * CopyOnWriteArraySets implement a java.util.Set that uses
7     * CopyOnWriteArrayList for all of its operations.
8     * Thus, it shares the same basic properties:
9     * <ul>
10     * <li> It is best suited for applications in which set sizes generally
11     * stay small, read-only operations
12     * vastly outnumber mutative operations, and you need
13     * to prevent interference among threads during traversal.
14     * <li> Mutative operations(add, set, remove, etc) are expensive
15     * since they usually entail copying the entire underlying array.
16     * <li> Loops involving repeated element-by-element mutative operations
17     * are so expensive that they should generally be avoided.
18     * <li> Iterators do not support the mutative remove operation
19     * <li> Traversal via iterators is very fast and cannot ever encounter
20     * interference from other threads. Iterators rely on
21     * unchanging snapshots of the array at the time the iterators were
22     * constructed.
23     * </ul>
24     * <p>
25     * <b>Sample Usage.</b> Probably the main application
26     * of copy-on-write sets are classes that maintain
27     * sets of Handler objects
28     * that must be multicasted to upon an update command. This
29     * is a classic case where you do not want to be holding a synch
30     * lock while sending a message, and where traversals normally
31     * vastly overwhelm additions.
32     * <pre>
33     * class Handler { void handle(); ... }
34     *
35     * class X {
36     * private final CopyOnWriteArraySet handlers = new CopyOnWriteArraySet();
37     * public void addHandler(Handler h) { handlers.add(h); }
38     *
39     * private long internalState;
40     * private synchronized void changeState() { internalState = ...; }
41     *
42     * public void update() {
43     * changeState();
44     * Iterator it = handlers.iterator();
45     * while (it.hasNext())
46     * ((Handler)(it.next()).handle();
47     * }
48     * }
49     * </pre>
50     * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
51     * @see CopyOnWriteArrayList
52     **/
53     public class CopyOnWriteArraySet<E> extends AbstractSet<E>
54     implements Cloneable, java.io.Serializable {
55    
56     private final CopyOnWriteArrayList<E> al;
57    
58     /**
59     * Constructs an empty set
60     */
61     public CopyOnWriteArraySet() {
62     al = new CopyOnWriteArrayList<E>();
63     }
64    
65     /**
66     * Constructs a set containing all of the elements of the specified
67     * Collection.
68     */
69     public <T extends E> CopyOnWriteArraySet(Collection<T> c) {
70     al = new CopyOnWriteArrayList<E>();
71     al.addAllAbsent(c);
72     }
73    
74    
75     public int size() { return al.size(); }
76     public boolean isEmpty() { return al.isEmpty(); }
77     public boolean contains(Object o) { return al.contains(o); }
78     public Object[] toArray() { return al.toArray(); }
79     public <T> T[] toArray(T[] a) { return al.toArray(a); }
80     public void clear() { al.clear(); }
81     public Iterator<E> iterator() { return al.iterator(); }
82     public boolean remove(Object o) { return al.remove(o); }
83     public boolean add(E o) { return al.addIfAbsent(o); }
84     public <T> boolean containsAll(Collection<T> c) { return al.containsAll(c); }
85     public <T extends E> boolean addAll(Collection<T> c) { return al.addAllAbsent(c) > 0; }
86     public <T> boolean removeAll(Collection<T> c) { return al.removeAll(c); }
87     public <T> boolean retainAll(Collection<T> c) { return al.retainAll(c); }
88    
89     }