Code - Class EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArraySet


1 /*
2   File: CopyOnWriteArraySet.java
3
4   Originally written by Doug Lea and released into the public domain.
5   This may be used for any purposes whatsoever without acknowledgment.
6   Thanks for the assistance and support of Sun Microsystems Labs,
7   and everyone contributing, testing, and using this code.
8
9   History:
10   Date Who What
11   22Jun1998 dl Create public version
12 */

13
14 package EDU.oswego.cs.dl.util.concurrent;
15 import java.util.*;
16
17 /**
18  * This class implements a java.util.Set that uses a
19  * CopyOnWriteArrayList for all of its operations.
20  * Thus, it shares the same basic properties:
21  * <ul>
22  * <li> It is best suited for applications in which set sizes generally
23  * stay small, read-only operations
24  * vastly outnumber mutative operations, and you need
25  * to prevent interference among threads during traversal.
26  * <li> Mutative operations(add, set, remove, etc) are fairly expensive
27  * since they usually entail copying the entire underlying array.
28  * <li> Loops involving repeated element-by-element mutative operations
29  * are so expensive that they should generally be avoided.
30  * <li> Iterators do not support the mutative remove operation
31  * <li> Traversal via iterators is very fast and cannot ever encounter
32  * interference from other threads. Iterators rely on
33  * unchanging snapshots of the array at the time the iterators were
34  * constructed
35  * </ul>
36  * <p>
37  * <b>Sample Usage.</b> Probably the main application
38  * of copy-on-write sets are classes that maintain
39  * sets of Handler objects
40  * that must be multicasted to upon an update command. This
41  * is a classic case where you do not want to be holding a synch
42  * lock while sending a message, and where traversals normally
43  * vastly overwhelm additions.
44  * <pre>
45  * class Handler { void handle(); ... }
46  *
47  * class X {
48  * private final CopyOnWriteArraySet handlers = new CopyOnWriteArraySet();
49  * public void addHandler(Handler h) { handlers.add(h); }
50  *
51  * private long internalState;
52  * private synchronized void changeState() { internalState = ...; }
53  *
54  * public void update() {
55  * changeState();
56  * Iterator it = handlers.iterator();
57  * while (it.hasNext())
58  * ((Handler)(it.next()).handle();
59  * }
60  * }
61  * </pre>
62  * <p>[<a HREF="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
63  * @see CopyOnWriteArrayList
64  **/

65
66
67 public class CopyOnWriteArraySet extends AbstractSet implements Cloneable, java.io.Serializable {
68
69   protected final CopyOnWriteArrayList al;
70
71   /**
72    * Constructs an empty set
73    */

74   public CopyOnWriteArraySet() {
75     al = new CopyOnWriteArrayList();
76   }
77
78   /**
79    * Constructs a set containing all of the elements of the specified
80    * Collection.
81    */

82   public CopyOnWriteArraySet(Collection c) {
83     al = new CopyOnWriteArrayList();
84     al.addAllAbsent(c);
85   }
86
87
88   public int size() { return al.size(); }
89   public boolean isEmpty() { return al.isEmpty(); }
90   public boolean contains(Object o) { return al.contains(o); }
91   public Object[] toArray() { return al.toArray(); }
92   public Object[] toArray(Object[] a) { return al.toArray(a); }
93   public void clear() { al.clear(); }
94   public Iterator iterator() { return al.iterator(); }
95   public boolean remove(Object o) { return al.remove(o); }
96   public boolean containsAll(Collection c) { return al.containsAll(c); }
97   public boolean addAll(Collection 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   public boolean add(Object o) { return al.addIfAbsent(o); }
101
102 }
103

Java API By Example, From Geeks To Geeks. | Conditions of Use | About Us © 2002 - 2005, KickJava.com, or its affiliates