42 * <ul> 43 * <li>It is best suited for applications in which set sizes generally 44 * stay small, read-only operations 45 * vastly outnumber mutative operations, and you need 46 * to prevent interference among threads during traversal. 47 * <li>It is thread-safe. 48 * <li>Mutative operations (<tt>add</tt>, <tt>set</tt>, <tt>remove</tt>, etc.) 49 * are expensive since they usually entail copying the entire underlying 50 * array. 51 * <li>Iterators do not support the mutative <tt>remove</tt> operation. 52 * <li>Traversal via iterators is fast and cannot encounter 53 * interference from other threads. Iterators rely on 54 * unchanging snapshots of the array at the time the iterators were 55 * constructed. 56 * </ul> 57 * 58 * <p> <b>Sample Usage.</b> The following code sketch uses a 59 * copy-on-write set to maintain a set of Handler objects that 60 * perform some action upon state updates. 61 * 62 * <pre> 63 * class Handler { void handle(); ... } 64 * 65 * class X { 66 * private final CopyOnWriteArraySet<Handler> handlers 67 * = new CopyOnWriteArraySet<Handler>(); 68 * public void addHandler(Handler h) { handlers.add(h); } 69 * 70 * private long internalState; 71 * private synchronized void changeState() { internalState = ...; } 72 * 73 * public void update() { 74 * changeState(); 75 * for (Handler handler : handlers) 76 * handler.handle(); 77 * } 78 * } 79 * </pre> 80 * 81 * <p>This class is a member of the 82 * <a href="{@docRoot}/../technotes/guides/collections/index.html"> 83 * Java Collections Framework</a>. 84 * 85 * @see CopyOnWriteArrayList 86 * @since 1.5 87 * @author Doug Lea 88 * @param <E> the type of elements held in this collection 89 */ 90 public class CopyOnWriteArraySet<E> extends AbstractSet<E> 91 implements java.io.Serializable { 92 private static final long serialVersionUID = 5457747651344034263L; 93 94 private final CopyOnWriteArrayList<E> al; 95 96 /** 97 * Creates an empty set. 98 */ 99 public CopyOnWriteArraySet() { | 42 * <ul> 43 * <li>It is best suited for applications in which set sizes generally 44 * stay small, read-only operations 45 * vastly outnumber mutative operations, and you need 46 * to prevent interference among threads during traversal. 47 * <li>It is thread-safe. 48 * <li>Mutative operations (<tt>add</tt>, <tt>set</tt>, <tt>remove</tt>, etc.) 49 * are expensive since they usually entail copying the entire underlying 50 * array. 51 * <li>Iterators do not support the mutative <tt>remove</tt> operation. 52 * <li>Traversal via iterators is fast and cannot encounter 53 * interference from other threads. Iterators rely on 54 * unchanging snapshots of the array at the time the iterators were 55 * constructed. 56 * </ul> 57 * 58 * <p> <b>Sample Usage.</b> The following code sketch uses a 59 * copy-on-write set to maintain a set of Handler objects that 60 * perform some action upon state updates. 61 * 62 * <pre> {@code 63 * class Handler { void handle(); ... } 64 * 65 * class X { 66 * private final CopyOnWriteArraySet<Handler> handlers 67 * = new CopyOnWriteArraySet<Handler>(); 68 * public void addHandler(Handler h) { handlers.add(h); } 69 * 70 * private long internalState; 71 * private synchronized void changeState() { internalState = ...; } 72 * 73 * public void update() { 74 * changeState(); 75 * for (Handler handler : handlers) 76 * handler.handle(); 77 * } 78 * }}</pre> 79 * 80 * <p>This class is a member of the 81 * <a href="{@docRoot}/../technotes/guides/collections/index.html"> 82 * Java Collections Framework</a>. 83 * 84 * @see CopyOnWriteArrayList 85 * @since 1.5 86 * @author Doug Lea 87 * @param <E> the type of elements held in this collection 88 */ 89 public class CopyOnWriteArraySet<E> extends AbstractSet<E> 90 implements java.io.Serializable { 91 private static final long serialVersionUID = 5457747651344034263L; 92 93 private final CopyOnWriteArrayList<E> al; 94 95 /** 96 * Creates an empty set. 97 */ 98 public CopyOnWriteArraySet() { |