ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CopyOnWriteArrayList.java
Revision: 1.52
Committed: Wed Sep 7 14:37:08 2005 UTC (18 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.51: +3 -1 lines
Log Message:
clone should reset lock

File Contents

# User Rev Content
1 dl 1.2 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.17 * Expert Group. Adapted and released, under explicit permission,
4 dl 1.15 * from JDK ArrayList.java which carries the following copyright:
5 dl 1.3 *
6     * Copyright 1997 by Sun Microsystems, Inc.,
7     * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
8     * All rights reserved.
9     *
10     * This software is the confidential and proprietary information
11     * of Sun Microsystems, Inc. ("Confidential Information"). You
12     * shall not disclose such Confidential Information and shall use
13     * it only in accordance with the terms of the license agreement
14     * you entered into with Sun.
15 dl 1.2 */
16    
17 tim 1.1 package java.util.concurrent;
18     import java.util.*;
19 dl 1.42 import java.util.concurrent.locks.*;
20 jsr166 1.45 import sun.misc.Unsafe;
21 tim 1.1
22     /**
23 dl 1.28 * A thread-safe variant of {@link java.util.ArrayList} in which all mutative
24 jsr166 1.35 * operations (<tt>add</tt>, <tt>set</tt>, and so on) are implemented by
25     * making a fresh copy of the underlying array.
26 tim 1.1 *
27 dl 1.26 * <p> This is ordinarily too costly, but may be <em>more</em> efficient
28 dl 1.12 * than alternatives when traversal operations vastly outnumber
29     * mutations, and is useful when you cannot or don't want to
30     * synchronize traversals, yet need to preclude interference among
31 dl 1.15 * concurrent threads. The "snapshot" style iterator method uses a
32     * reference to the state of the array at the point that the iterator
33     * was created. This array never changes during the lifetime of the
34     * iterator, so interference is impossible and the iterator is
35     * guaranteed not to throw <tt>ConcurrentModificationException</tt>.
36     * The iterator will not reflect additions, removals, or changes to
37     * the list since the iterator was created. Element-changing
38 jsr166 1.35 * operations on iterators themselves (<tt>remove</tt>, <tt>set</tt>, and
39     * <tt>add</tt>) are not supported. These methods throw
40 dl 1.15 * <tt>UnsupportedOperationException</tt>.
41 dl 1.24 *
42 jsr166 1.35 * <p>All elements are permitted, including <tt>null</tt>.
43     *
44 dl 1.24 * <p>This class is a member of the
45     * <a href="{@docRoot}/../guide/collections/index.html">
46     * Java Collections Framework</a>.
47     *
48 brian 1.51 * <p>
49     * Memory visibility effects: As with other concurrent collections, state
50     * changes to any object made prior to placing it into a <tt>CopyOnWriteArrayList</tt>
51     * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
52     * that element is accessed via or removed from the <tt>CopyOnWriteArrayList</tt>.
53     *
54 dl 1.6 * @since 1.5
55     * @author Doug Lea
56 dl 1.19 * @param <E> the type of elements held in this collection
57 dl 1.6 */
58 tim 1.1 public class CopyOnWriteArrayList<E>
59 dl 1.40 implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
60 dl 1.11 private static final long serialVersionUID = 8673264195747942595L;
61 tim 1.1
62 dl 1.42 /** The lock protecting all mutators */
63     transient final ReentrantLock lock = new ReentrantLock();
64    
65 dl 1.40 /** The array, accessed only via getArray/setArray. */
66 dl 1.41 private volatile transient Object[] array;
67 tim 1.1
68 dl 1.42 Object[] getArray() { return array; }
69     void setArray(Object[] a) { array = a; }
70 tim 1.1
71     /**
72 dl 1.15 * Creates an empty list.
73 tim 1.1 */
74     public CopyOnWriteArrayList() {
75 dl 1.41 setArray(new Object[0]);
76 tim 1.1 }
77    
78     /**
79 dl 1.15 * Creates a list containing the elements of the specified
80 jsr166 1.32 * collection, in the order they are returned by the collection's
81 tim 1.1 * iterator.
82 jsr166 1.32 *
83 dl 1.6 * @param c the collection of initially held elements
84 jsr166 1.35 * @throws NullPointerException if the specified collection is null
85 tim 1.1 */
86 tim 1.22 public CopyOnWriteArrayList(Collection<? extends E> c) {
87 dl 1.41 Object[] elements = new Object[c.size()];
88 jsr166 1.45 int size = 0;
89     for (E e : c)
90     elements[size++] = e;
91     setArray(elements);
92 tim 1.1 }
93    
94     /**
95 jsr166 1.35 * Creates a list holding a copy of the given array.
96 tim 1.9 *
97     * @param toCopyIn the array (a copy of this array is used as the
98     * internal array)
99 jsr166 1.38 * @throws NullPointerException if the specified array is null
100 jsr166 1.30 */
101 tim 1.1 public CopyOnWriteArrayList(E[] toCopyIn) {
102 jsr166 1.45 setArray(Arrays.copyOf(toCopyIn, toCopyIn.length, Object[].class));
103 tim 1.1 }
104    
105     /**
106 dl 1.15 * Returns the number of elements in this list.
107 tim 1.1 *
108 jsr166 1.35 * @return the number of elements in this list
109 tim 1.1 */
110     public int size() {
111 dl 1.40 return getArray().length;
112 tim 1.1 }
113    
114     /**
115 jsr166 1.35 * Returns <tt>true</tt> if this list contains no elements.
116 tim 1.1 *
117 jsr166 1.35 * @return <tt>true</tt> if this list contains no elements
118 tim 1.1 */
119     public boolean isEmpty() {
120     return size() == 0;
121     }
122    
123 dl 1.43 /**
124     * Test for equality, coping with nulls.
125     */
126     private static boolean eq(Object o1, Object o2) {
127     return (o1 == null ? o2 == null : o1.equals(o2));
128     }
129    
130 dl 1.41 /**
131 dl 1.40 * static version of indexOf, to allow repeated calls without
132 dl 1.41 * needing to re-acquire array each time.
133 dl 1.40 * @param o element to search for
134     * @param elements the array
135     * @param index first index to search
136     * @param fence one past last index to search
137     * @return index of element, or -1 if absent
138     */
139 dl 1.41 private static int indexOf(Object o, Object[] elements,
140 dl 1.40 int index, int fence) {
141     if (o == null) {
142     for (int i = index; i < fence; i++)
143     if (elements[i] == null)
144     return i;
145     } else {
146     for (int i = index; i < fence; i++)
147     if (o.equals(elements[i]))
148     return i;
149     }
150     return -1;
151     }
152 dl 1.41
153     /**
154 dl 1.40 * static version of lastIndexOf.
155     * @param o element to search for
156     * @param elements the array
157     * @param index first index to search
158     * @return index of element, or -1 if absent
159     */
160     private static int lastIndexOf(Object o, Object[] elements, int index) {
161     if (o == null) {
162     for (int i = index; i >= 0; i--)
163     if (elements[i] == null)
164     return i;
165     } else {
166     for (int i = index; i >= 0; i--)
167     if (o.equals(elements[i]))
168     return i;
169     }
170     return -1;
171     }
172    
173 tim 1.1 /**
174 dl 1.15 * Returns <tt>true</tt> if this list contains the specified element.
175 jsr166 1.35 * More formally, returns <tt>true</tt> if and only if this list contains
176     * at least one element <tt>e</tt> such that
177     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
178 tim 1.1 *
179 jsr166 1.35 * @param o element whose presence in this list is to be tested
180     * @return <tt>true</tt> if this list contains the specified element
181 tim 1.1 */
182 jsr166 1.35 public boolean contains(Object o) {
183 dl 1.41 Object[] elements = getArray();
184 dl 1.40 return indexOf(o, elements, 0, elements.length) >= 0;
185 tim 1.1 }
186    
187     /**
188 jsr166 1.35 * {@inheritDoc}
189 tim 1.1 */
190 jsr166 1.35 public int indexOf(Object o) {
191 dl 1.41 Object[] elements = getArray();
192 dl 1.40 return indexOf(o, elements, 0, elements.length);
193 tim 1.1 }
194    
195     /**
196 jsr166 1.35 * Returns the index of the first occurrence of the specified element in
197     * this list, searching forwards from <tt>index</tt>, or returns -1 if
198     * the element is not found.
199     * More formally, returns the lowest index <tt>i</tt> such that
200     * <tt>(i&nbsp;&gt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(e==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;e.equals(get(i))))</tt>,
201     * or -1 if there is no such index.
202     *
203     * @param e element to search for
204     * @param index index to start searching from
205     * @return the index of the first occurrence of the element in
206     * this list at position <tt>index</tt> or later in the list;
207     * <tt>-1</tt> if the element is not found.
208     * @throws IndexOutOfBoundsException if the specified index is negative
209 tim 1.1 */
210 jsr166 1.35 public int indexOf(E e, int index) {
211 dl 1.41 Object[] elements = getArray();
212 jsr166 1.45 return indexOf(e, elements, index, elements.length);
213 tim 1.1 }
214    
215     /**
216 jsr166 1.35 * {@inheritDoc}
217 tim 1.1 */
218 jsr166 1.35 public int lastIndexOf(Object o) {
219 dl 1.41 Object[] elements = getArray();
220 dl 1.40 return lastIndexOf(o, elements, elements.length - 1);
221 tim 1.1 }
222    
223     /**
224 jsr166 1.35 * Returns the index of the last occurrence of the specified element in
225     * this list, searching backwards from <tt>index</tt>, or returns -1 if
226     * the element is not found.
227     * More formally, returns the highest index <tt>i</tt> such that
228     * <tt>(i&nbsp;&lt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(e==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;e.equals(get(i))))</tt>,
229     * or -1 if there is no such index.
230     *
231     * @param e element to search for
232     * @param index index to start searching backwards from
233     * @return the index of the last occurrence of the element at position
234     * less than or equal to <tt>index</tt> in this list;
235     * -1 if the element is not found.
236     * @throws IndexOutOfBoundsException if the specified index is greater
237     * than or equal to the current size of this list
238 tim 1.1 */
239 jsr166 1.35 public int lastIndexOf(E e, int index) {
240 dl 1.41 Object[] elements = getArray();
241 jsr166 1.45 return lastIndexOf(e, elements, index);
242 tim 1.1 }
243    
244     /**
245     * Returns a shallow copy of this list. (The elements themselves
246     * are not copied.)
247     *
248 jsr166 1.35 * @return a clone of this list
249 tim 1.1 */
250     public Object clone() {
251     try {
252 dl 1.52 CopyOnWriteArrayList c = (CopyOnWriteArrayList)(super.clone());
253     c.resetLock();
254     return c;
255 tim 1.1 } catch (CloneNotSupportedException e) {
256     // this shouldn't happen, since we are Cloneable
257     throw new InternalError();
258     }
259     }
260    
261     /**
262     * Returns an array containing all of the elements in this list
263 jsr166 1.35 * in proper sequence (from first to last element).
264     *
265     * <p>The returned array will be "safe" in that no references to it are
266     * maintained by this list. (In other words, this method must allocate
267     * a new array). The caller is thus free to modify the returned array.
268 jsr166 1.36 *
269 jsr166 1.35 * <p>This method acts as bridge between array-based and collection-based
270     * APIs.
271     *
272     * @return an array containing all the elements in this list
273 tim 1.1 */
274     public Object[] toArray() {
275 dl 1.40 Object[] elements = getArray();
276 jsr166 1.45 return Arrays.copyOf(elements, elements.length);
277 tim 1.1 }
278    
279     /**
280 jsr166 1.35 * Returns an array containing all of the elements in this list in
281     * proper sequence (from first to last element); the runtime type of
282     * the returned array is that of the specified array. If the list fits
283     * in the specified array, it is returned therein. Otherwise, a new
284     * array is allocated with the runtime type of the specified array and
285     * the size of this list.
286 jsr166 1.32 *
287     * <p>If this list fits in the specified array with room to spare
288 jsr166 1.35 * (i.e., the array has more elements than this list), the element in
289     * the array immediately following the end of the list is set to
290     * <tt>null</tt>. (This is useful in determining the length of this
291     * list <i>only</i> if the caller knows that this list does not contain
292     * any null elements.)
293     *
294     * <p>Like the {@link #toArray()} method, this method acts as bridge between
295     * array-based and collection-based APIs. Further, this method allows
296     * precise control over the runtime type of the output array, and may,
297     * under certain circumstances, be used to save allocation costs.
298     *
299     * <p>Suppose <tt>x</tt> is a list known to contain only strings.
300     * The following code can be used to dump the list into a newly
301     * allocated array of <tt>String</tt>:
302     *
303     * <pre>
304     * String[] y = x.toArray(new String[0]);</pre>
305     *
306     * Note that <tt>toArray(new Object[0])</tt> is identical in function to
307     * <tt>toArray()</tt>.
308 tim 1.1 *
309     * @param a the array into which the elements of the list are to
310 jsr166 1.35 * be stored, if it is big enough; otherwise, a new array of the
311     * same runtime type is allocated for this purpose.
312     * @return an array containing all the elements in this list
313     * @throws ArrayStoreException if the runtime type of the specified array
314     * is not a supertype of the runtime type of every element in
315     * this list
316     * @throws NullPointerException if the specified array is null
317 tim 1.1 */
318     public <T> T[] toArray(T a[]) {
319 dl 1.41 Object[] elements = getArray();
320 dl 1.40 int len = elements.length;
321     if (a.length < len)
322 jsr166 1.45 return (T[]) Arrays.copyOf(elements, len, a.getClass());
323     else {
324     System.arraycopy(elements, 0, a, 0, len);
325     if (a.length > len)
326     a[len] = null;
327     return a;
328     }
329 tim 1.1 }
330    
331     // Positional Access Operations
332    
333     /**
334 jsr166 1.35 * {@inheritDoc}
335 tim 1.1 *
336 jsr166 1.35 * @throws IndexOutOfBoundsException {@inheritDoc}
337 tim 1.1 */
338     public E get(int index) {
339 dl 1.41 return (E)(getArray()[index]);
340 tim 1.1 }
341    
342     /**
343 jsr166 1.35 * Replaces the element at the specified position in this list with the
344     * specified element.
345 tim 1.1 *
346 jsr166 1.35 * @throws IndexOutOfBoundsException {@inheritDoc}
347 tim 1.1 */
348 dl 1.42 public E set(int index, E element) {
349 jsr166 1.45 final ReentrantLock lock = this.lock;
350     lock.lock();
351     try {
352     Object[] elements = getArray();
353     int len = elements.length;
354     Object oldValue = elements[index];
355    
356     if (oldValue != element) {
357     Object[] newElements = Arrays.copyOf(elements, len);
358     newElements[index] = element;
359     setArray(newElements);
360     }
361     return (E)oldValue;
362     } finally {
363     lock.unlock();
364     }
365 tim 1.1 }
366    
367     /**
368     * Appends the specified element to the end of this list.
369     *
370 dl 1.40 * @param e element to be appended to this list
371 jsr166 1.49 * @return <tt>true</tt> (as specified by {@link Collection#add})
372 tim 1.1 */
373 dl 1.42 public boolean add(E e) {
374 jsr166 1.45 final ReentrantLock lock = this.lock;
375     lock.lock();
376     try {
377     Object[] elements = getArray();
378     int len = elements.length;
379     Object[] newElements = Arrays.copyOf(elements, len + 1);
380     newElements[len] = e;
381     setArray(newElements);
382     return true;
383     } finally {
384     lock.unlock();
385     }
386 tim 1.1 }
387    
388     /**
389     * Inserts the specified element at the specified position in this
390     * list. Shifts the element currently at that position (if any) and
391     * any subsequent elements to the right (adds one to their indices).
392     *
393 jsr166 1.35 * @throws IndexOutOfBoundsException {@inheritDoc}
394 tim 1.1 */
395 dl 1.42 public void add(int index, E element) {
396 jsr166 1.45 final ReentrantLock lock = this.lock;
397     lock.lock();
398     try {
399     Object[] elements = getArray();
400     int len = elements.length;
401     if (index > len || index < 0)
402     throw new IndexOutOfBoundsException("Index: "+index+
403     ", Size: "+len);
404     Object[] newElements;
405     int numMoved = len - index;
406     if (numMoved == 0)
407     newElements = Arrays.copyOf(elements, len + 1);
408     else {
409     newElements = new Object[len + 1];
410     System.arraycopy(elements, 0, newElements, 0, index);
411     System.arraycopy(elements, index, newElements, index + 1,
412     numMoved);
413     }
414     newElements[index] = element;
415     setArray(newElements);
416     } finally {
417     lock.unlock();
418     }
419 tim 1.1 }
420    
421     /**
422     * Removes the element at the specified position in this list.
423     * Shifts any subsequent elements to the left (subtracts one from their
424 jsr166 1.35 * indices). Returns the element that was removed from the list.
425 tim 1.1 *
426 jsr166 1.35 * @throws IndexOutOfBoundsException {@inheritDoc}
427 tim 1.1 */
428 dl 1.42 public E remove(int index) {
429 jsr166 1.45 final ReentrantLock lock = this.lock;
430     lock.lock();
431     try {
432     Object[] elements = getArray();
433     int len = elements.length;
434     Object oldValue = elements[index];
435     int numMoved = len - index - 1;
436     if (numMoved == 0)
437     setArray(Arrays.copyOf(elements, len - 1));
438     else {
439     Object[] newElements = new Object[len - 1];
440     System.arraycopy(elements, 0, newElements, 0, index);
441     System.arraycopy(elements, index + 1, newElements, index,
442     numMoved);
443     setArray(newElements);
444     }
445     return (E)oldValue;
446     } finally {
447     lock.unlock();
448     }
449 tim 1.1 }
450    
451     /**
452 jsr166 1.35 * Removes the first occurrence of the specified element from this list,
453     * if it is present. If this list does not contain the element, it is
454     * unchanged. More formally, removes the element with the lowest index
455     * <tt>i</tt> such that
456     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
457     * (if such an element exists). Returns <tt>true</tt> if this list
458     * contained the specified element (or equivalently, if this list
459     * changed as a result of the call).
460 tim 1.1 *
461 jsr166 1.35 * @param o element to be removed from this list, if present
462     * @return <tt>true</tt> if this list contained the specified element
463 tim 1.1 */
464 dl 1.42 public boolean remove(Object o) {
465 jsr166 1.45 final ReentrantLock lock = this.lock;
466     lock.lock();
467     try {
468     Object[] elements = getArray();
469     int len = elements.length;
470     if (len != 0) {
471     // Copy while searching for element to remove
472     // This wins in the normal case of element being present
473     int newlen = len - 1;
474     Object[] newElements = new Object[newlen];
475    
476     for (int i = 0; i < newlen; ++i) {
477     if (eq(o, elements[i])) {
478     // found one; copy remaining and exit
479     for (int k = i + 1; k < len; ++k)
480     newElements[k-1] = elements[k];
481     setArray(newElements);
482     return true;
483     } else
484     newElements[i] = elements[i];
485     }
486    
487     // special handling for last cell
488     if (eq(o, elements[newlen])) {
489     setArray(newElements);
490     return true;
491     }
492     }
493     return false;
494     } finally {
495     lock.unlock();
496     }
497 tim 1.1 }
498    
499     /**
500 jsr166 1.35 * Removes from this list all of the elements whose index is between
501     * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive.
502     * Shifts any succeeding elements to the left (reduces their index).
503 dl 1.15 * This call shortens the list by <tt>(toIndex - fromIndex)</tt> elements.
504     * (If <tt>toIndex==fromIndex</tt>, this operation has no effect.)
505 tim 1.1 *
506 jsr166 1.35 * @param fromIndex index of first element to be removed
507     * @param toIndex index after last element to be removed
508 dl 1.29 * @throws IndexOutOfBoundsException if fromIndex or toIndex out of
509 tim 1.1 * range (fromIndex &lt; 0 || fromIndex &gt;= size() || toIndex
510 jsr166 1.35 * &gt; size() || toIndex &lt; fromIndex)
511 tim 1.1 */
512 dl 1.42 private void removeRange(int fromIndex, int toIndex) {
513 jsr166 1.45 final ReentrantLock lock = this.lock;
514     lock.lock();
515     try {
516     Object[] elements = getArray();
517     int len = elements.length;
518    
519     if (fromIndex < 0 || fromIndex >= len ||
520     toIndex > len || toIndex < fromIndex)
521     throw new IndexOutOfBoundsException();
522     int newlen = len - (toIndex - fromIndex);
523     int numMoved = len - toIndex;
524     if (numMoved == 0)
525     setArray(Arrays.copyOf(elements, newlen));
526     else {
527     Object[] newElements = new Object[newlen];
528     System.arraycopy(elements, 0, newElements, 0, fromIndex);
529     System.arraycopy(elements, toIndex, newElements,
530     fromIndex, numMoved);
531     setArray(newElements);
532     }
533     } finally {
534     lock.unlock();
535     }
536 tim 1.1 }
537    
538     /**
539     * Append the element if not present.
540 jsr166 1.38 *
541 dl 1.40 * @param e element to be added to this list, if absent
542 jsr166 1.38 * @return <tt>true</tt> if the element was added
543 jsr166 1.30 */
544 dl 1.42 public boolean addIfAbsent(E e) {
545 jsr166 1.45 final ReentrantLock lock = this.lock;
546     lock.lock();
547     try {
548     // Copy while checking if already present.
549     // This wins in the most common case where it is not present
550     Object[] elements = getArray();
551     int len = elements.length;
552     Object[] newElements = new Object[len + 1];
553     for (int i = 0; i < len; ++i) {
554     if (eq(e, elements[i]))
555     return false; // exit, throwing away copy
556     else
557     newElements[i] = elements[i];
558     }
559     newElements[len] = e;
560     setArray(newElements);
561     return true;
562     } finally {
563     lock.unlock();
564     }
565 tim 1.1 }
566    
567     /**
568 jsr166 1.35 * Returns <tt>true</tt> if this list contains all of the elements of the
569 jsr166 1.32 * specified collection.
570 jsr166 1.34 *
571 jsr166 1.36 * @param c collection to be checked for containment in this list
572 jsr166 1.35 * @return <tt>true</tt> if this list contains all of the elements of the
573     * specified collection
574     * @throws NullPointerException if the specified collection is null
575 jsr166 1.38 * @see #contains(Object)
576 tim 1.1 */
577 tim 1.7 public boolean containsAll(Collection<?> c) {
578 dl 1.41 Object[] elements = getArray();
579 dl 1.40 int len = elements.length;
580 jsr166 1.45 for (Object e : c) {
581 dl 1.41 if (indexOf(e, elements, 0, len) < 0)
582 tim 1.1 return false;
583 jsr166 1.45 }
584 tim 1.1 return true;
585     }
586    
587     /**
588 jsr166 1.32 * Removes from this list all of its elements that are contained in
589     * the specified collection. This is a particularly expensive operation
590 tim 1.1 * in this class because of the need for an internal temporary array.
591     *
592 jsr166 1.35 * @param c collection containing elements to be removed from this list
593     * @return <tt>true</tt> if this list changed as a result of the call
594 jsr166 1.38 * @throws ClassCastException if the class of an element of this list
595     * is incompatible with the specified collection (optional)
596     * @throws NullPointerException if this list contains a null element and the
597     * specified collection does not permit null elements (optional),
598     * or if the specified collection is null
599     * @see #remove(Object)
600 tim 1.1 */
601 dl 1.42 public boolean removeAll(Collection<?> c) {
602 jsr166 1.45 final ReentrantLock lock = this.lock;
603     lock.lock();
604     try {
605     Object[] elements = getArray();
606     int len = elements.length;
607     if (len != 0) {
608     // temp array holds those elements we know we want to keep
609     int newlen = 0;
610     Object[] temp = new Object[len];
611     for (int i = 0; i < len; ++i) {
612     Object element = elements[i];
613     if (!c.contains(element))
614     temp[newlen++] = element;
615     }
616     if (newlen != len) {
617     setArray(Arrays.copyOf(temp, newlen));
618     return true;
619     }
620     }
621     return false;
622     } finally {
623     lock.unlock();
624     }
625 tim 1.1 }
626    
627     /**
628 jsr166 1.32 * Retains only the elements in this list that are contained in the
629 jsr166 1.35 * specified collection. In other words, removes from this list all of
630     * its elements that are not contained in the specified collection.
631 jsr166 1.32 *
632 jsr166 1.35 * @param c collection containing elements to be retained in this list
633     * @return <tt>true</tt> if this list changed as a result of the call
634 jsr166 1.38 * @throws ClassCastException if the class of an element of this list
635     * is incompatible with the specified collection (optional)
636     * @throws NullPointerException if this list contains a null element and the
637     * specified collection does not permit null elements (optional),
638     * or if the specified collection is null
639     * @see #remove(Object)
640 tim 1.1 */
641 dl 1.42 public boolean retainAll(Collection<?> c) {
642 jsr166 1.45 final ReentrantLock lock = this.lock;
643     lock.lock();
644     try {
645     Object[] elements = getArray();
646     int len = elements.length;
647     if (len != 0) {
648     // temp array holds those elements we know we want to keep
649     int newlen = 0;
650     Object[] temp = new Object[len];
651     for (int i = 0; i < len; ++i) {
652     Object element = elements[i];
653     if (c.contains(element))
654     temp[newlen++] = element;
655     }
656     if (newlen != len) {
657     setArray(Arrays.copyOf(temp, newlen));
658     return true;
659     }
660     }
661     return false;
662     } finally {
663     lock.unlock();
664     }
665 tim 1.1 }
666    
667     /**
668 jsr166 1.32 * Appends all of the elements in the specified collection that
669 tim 1.1 * are not already contained in this list, to the end of
670     * this list, in the order that they are returned by the
671 jsr166 1.32 * specified collection's iterator.
672 tim 1.1 *
673 jsr166 1.36 * @param c collection containing elements to be added to this list
674 tim 1.1 * @return the number of elements added
675 jsr166 1.35 * @throws NullPointerException if the specified collection is null
676 jsr166 1.38 * @see #addIfAbsent(Object)
677 tim 1.1 */
678 dl 1.42 public int addAllAbsent(Collection<? extends E> c) {
679 jsr166 1.45 int numNew = c.size();
680     if (numNew == 0)
681     return 0;
682     final ReentrantLock lock = this.lock;
683     lock.lock();
684     try {
685     Object[] elements = getArray();
686     int len = elements.length;
687    
688     Object[] temp = new Object[numNew];
689     int added = 0;
690     for (E e : c) {
691     if (indexOf(e, elements, 0, len) < 0 &&
692     indexOf(e, temp, 0, added) < 0)
693     temp[added++] = e;
694     }
695     if (added != 0) {
696     Object[] newElements = Arrays.copyOf(elements, len + added);
697     System.arraycopy(temp, 0, newElements, len, added);
698     setArray(newElements);
699     }
700     return added;
701     } finally {
702     lock.unlock();
703     }
704 tim 1.1 }
705    
706     /**
707     * Removes all of the elements from this list.
708 jsr166 1.38 * The list will be empty after this call returns.
709 tim 1.1 */
710 dl 1.42 public void clear() {
711 jsr166 1.45 final ReentrantLock lock = this.lock;
712     lock.lock();
713     try {
714     setArray(new Object[0]);
715     } finally {
716     lock.unlock();
717     }
718 tim 1.1 }
719    
720     /**
721 jsr166 1.32 * Appends all of the elements in the specified collection to the end
722     * of this list, in the order that they are returned by the specified
723     * collection's iterator.
724 tim 1.1 *
725 jsr166 1.36 * @param c collection containing elements to be added to this list
726 jsr166 1.38 * @return <tt>true</tt> if this list changed as a result of the call
727 jsr166 1.35 * @throws NullPointerException if the specified collection is null
728 jsr166 1.38 * @see #add(Object)
729 tim 1.1 */
730 dl 1.42 public boolean addAll(Collection<? extends E> c) {
731 jsr166 1.45 int numNew = c.size();
732     if (numNew == 0)
733     return false;
734     final ReentrantLock lock = this.lock;
735     lock.lock();
736     try {
737     Object[] elements = getArray();
738     int len = elements.length;
739     Object[] newElements = Arrays.copyOf(elements, len + numNew);
740     for (E e : c)
741     newElements[len++] = e;
742     setArray(newElements);
743     return true;
744     } finally {
745     lock.unlock();
746     }
747 tim 1.1 }
748    
749     /**
750 jsr166 1.35 * Inserts all of the elements in the specified collection into this
751 tim 1.1 * list, starting at the specified position. Shifts the element
752     * currently at that position (if any) and any subsequent elements to
753     * the right (increases their indices). The new elements will appear
754 jsr166 1.38 * in this list in the order that they are returned by the
755     * specified collection's iterator.
756 tim 1.1 *
757 jsr166 1.35 * @param index index at which to insert the first element
758     * from the specified collection
759 jsr166 1.36 * @param c collection containing elements to be added to this list
760 jsr166 1.35 * @return <tt>true</tt> if this list changed as a result of the call
761     * @throws IndexOutOfBoundsException {@inheritDoc}
762     * @throws NullPointerException if the specified collection is null
763 jsr166 1.38 * @see #add(int,Object)
764 tim 1.1 */
765 dl 1.42 public boolean addAll(int index, Collection<? extends E> c) {
766 jsr166 1.45 int numNew = c.size();
767     final ReentrantLock lock = this.lock;
768     lock.lock();
769     try {
770     Object[] elements = getArray();
771     int len = elements.length;
772     if (index > len || index < 0)
773     throw new IndexOutOfBoundsException("Index: "+index+
774     ", Size: "+len);
775 dl 1.46 if (numNew == 0)
776     return false;
777 jsr166 1.45 int numMoved = len - index;
778     Object[] newElements;
779     if (numMoved == 0)
780     newElements = Arrays.copyOf(elements, len + numNew);
781     else {
782     newElements = new Object[len + numNew];
783     System.arraycopy(elements, 0, newElements, 0, index);
784     System.arraycopy(elements, index,
785     newElements, index + numNew,
786     numMoved);
787     }
788     for (E e : c)
789     newElements[index++] = e;
790     setArray(newElements);
791     return true;
792     } finally {
793     lock.unlock();
794     }
795 tim 1.1 }
796    
797     /**
798     * Save the state of the list to a stream (i.e., serialize it).
799     *
800     * @serialData The length of the array backing the list is emitted
801     * (int), followed by all of its elements (each an Object)
802     * in the proper order.
803 dl 1.6 * @param s the stream
804 tim 1.1 */
805     private void writeObject(java.io.ObjectOutputStream s)
806     throws java.io.IOException{
807    
808     // Write out element count, and any hidden stuff
809     s.defaultWriteObject();
810    
811 dl 1.41 Object[] elements = getArray();
812 jsr166 1.45 int len = elements.length;
813 tim 1.1 // Write out array length
814 dl 1.40 s.writeInt(len);
815 tim 1.1
816     // Write out all elements in the proper order.
817 dl 1.40 for (int i = 0; i < len; i++)
818     s.writeObject(elements[i]);
819 tim 1.1 }
820    
821     /**
822     * Reconstitute the list from a stream (i.e., deserialize it).
823 dl 1.6 * @param s the stream
824 tim 1.1 */
825 dl 1.12 private void readObject(java.io.ObjectInputStream s)
826 tim 1.1 throws java.io.IOException, ClassNotFoundException {
827    
828     // Read in size, and any hidden stuff
829     s.defaultReadObject();
830    
831 dl 1.42 // bind to new lock
832     resetLock();
833    
834 tim 1.1 // Read in array length and allocate array
835 dl 1.40 int len = s.readInt();
836 dl 1.41 Object[] elements = new Object[len];
837 tim 1.1
838     // Read in all elements in the proper order.
839 dl 1.40 for (int i = 0; i < len; i++)
840 dl 1.41 elements[i] = s.readObject();
841 dl 1.40 setArray(elements);
842 tim 1.1 }
843    
844     /**
845 jsr166 1.32 * Returns a string representation of this list, containing
846 tim 1.1 * the String representation of each element.
847     */
848     public String toString() {
849 jsr166 1.45 Object[] elements = getArray();
850     int maxIndex = elements.length - 1;
851 dl 1.43 StringBuilder sb = new StringBuilder();
852     sb.append("[");
853 tim 1.1 for (int i = 0; i <= maxIndex; i++) {
854 dl 1.43 sb.append(String.valueOf(elements[i]));
855 tim 1.1 if (i < maxIndex)
856 dl 1.43 sb.append(", ");
857 tim 1.1 }
858 dl 1.43 sb.append("]");
859     return sb.toString();
860 tim 1.1 }
861    
862     /**
863 jsr166 1.35 * Compares the specified object with this list for equality.
864     * Returns true if and only if the specified object is also a {@link
865     * List}, both lists have the same size, and all corresponding pairs
866     * of elements in the two lists are <em>equal</em>. (Two elements
867     * <tt>e1</tt> and <tt>e2</tt> are <em>equal</em> if <tt>(e1==null ?
868     * e2==null : e1.equals(e2))</tt>.) In other words, two lists are
869     * defined to be equal if they contain the same elements in the same
870     * order.
871 tim 1.1 *
872 jsr166 1.35 * @param o the object to be compared for equality with this list
873     * @return <tt>true</tt> if the specified object is equal to this list
874 tim 1.1 */
875     public boolean equals(Object o) {
876     if (o == this)
877     return true;
878     if (!(o instanceof List))
879     return false;
880    
881 dl 1.43 List<?> l2 = (List<?>)(o);
882 tim 1.1 if (size() != l2.size())
883     return false;
884    
885 dl 1.41 ListIterator<?> e1 = listIterator();
886     ListIterator<?> e2 = l2.listIterator();
887 jsr166 1.35 while (e1.hasNext()) {
888 dl 1.43 if (!eq(e1.next(), e2.next()))
889 tim 1.1 return false;
890     }
891     return true;
892     }
893    
894     /**
895 jsr166 1.35 * Returns the hash code value for this list.
896 dl 1.26 *
897 jsr166 1.47 * <p>This implementation uses the definition in {@link List#hashCode}.
898     *
899     * @return the hash code value for this list
900 tim 1.1 */
901     public int hashCode() {
902     int hashCode = 1;
903 jsr166 1.45 Object[] elements = getArray();
904     int len = elements.length;
905     for (int i = 0; i < len; ++i) {
906     Object obj = elements[i];
907     hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
908 tim 1.1 }
909     return hashCode;
910     }
911    
912     /**
913 jsr166 1.35 * Returns an iterator over the elements in this list in proper sequence.
914     *
915     * <p>The returned iterator provides a snapshot of the state of the list
916     * when the iterator was constructed. No synchronization is needed while
917     * traversing the iterator. The iterator does <em>NOT</em> support the
918     * <tt>remove</tt> method.
919     *
920     * @return an iterator over the elements in this list in proper sequence
921 tim 1.1 */
922     public Iterator<E> iterator() {
923 dl 1.40 return new COWIterator<E>(getArray(), 0);
924 tim 1.1 }
925    
926     /**
927 jsr166 1.35 * {@inheritDoc}
928 tim 1.1 *
929 jsr166 1.35 * <p>The returned iterator provides a snapshot of the state of the list
930     * when the iterator was constructed. No synchronization is needed while
931     * traversing the iterator. The iterator does <em>NOT</em> support the
932     * <tt>remove</tt>, <tt>set</tt> or <tt>add</tt> methods.
933 tim 1.1 */
934     public ListIterator<E> listIterator() {
935 dl 1.40 return new COWIterator<E>(getArray(), 0);
936 tim 1.1 }
937    
938     /**
939 jsr166 1.35 * {@inheritDoc}
940     *
941 jsr166 1.50 * <p>The returned iterator provides a snapshot of the state of the list
942     * when the iterator was constructed. No synchronization is needed while
943     * traversing the iterator. The iterator does <em>NOT</em> support the
944     * <tt>remove</tt>, <tt>set</tt> or <tt>add</tt> methods.
945 jsr166 1.35 *
946     * @throws IndexOutOfBoundsException {@inheritDoc}
947 tim 1.1 */
948     public ListIterator<E> listIterator(final int index) {
949 dl 1.41 Object[] elements = getArray();
950 dl 1.40 int len = elements.length;
951 jsr166 1.45 if (index<0 || index>len)
952     throw new IndexOutOfBoundsException("Index: "+index);
953 tim 1.1
954 dl 1.48 return new COWIterator<E>(elements, index);
955 tim 1.1 }
956    
957     private static class COWIterator<E> implements ListIterator<E> {
958     /** Snapshot of the array **/
959 dl 1.41 private final Object[] snapshot;
960 dl 1.40 /** Index of element to be returned by subsequent call to next. */
961 tim 1.1 private int cursor;
962    
963 dl 1.41 private COWIterator(Object[] elements, int initialCursor) {
964 tim 1.1 cursor = initialCursor;
965 dl 1.40 snapshot = elements;
966 tim 1.1 }
967    
968     public boolean hasNext() {
969 dl 1.40 return cursor < snapshot.length;
970 tim 1.1 }
971    
972     public boolean hasPrevious() {
973     return cursor > 0;
974     }
975    
976     public E next() {
977     try {
978 dl 1.41 return (E)(snapshot[cursor++]);
979 tim 1.10 } catch (IndexOutOfBoundsException ex) {
980 tim 1.1 throw new NoSuchElementException();
981     }
982     }
983    
984     public E previous() {
985     try {
986 dl 1.41 return (E)(snapshot[--cursor]);
987 jsr166 1.30 } catch (IndexOutOfBoundsException e) {
988 tim 1.1 throw new NoSuchElementException();
989     }
990     }
991    
992     public int nextIndex() {
993     return cursor;
994     }
995    
996     public int previousIndex() {
997 jsr166 1.45 return cursor-1;
998 tim 1.1 }
999    
1000     /**
1001     * Not supported. Always throws UnsupportedOperationException.
1002 jsr166 1.32 * @throws UnsupportedOperationException always; <tt>remove</tt>
1003     * is not supported by this iterator.
1004 tim 1.1 */
1005     public void remove() {
1006     throw new UnsupportedOperationException();
1007     }
1008    
1009     /**
1010     * Not supported. Always throws UnsupportedOperationException.
1011 jsr166 1.32 * @throws UnsupportedOperationException always; <tt>set</tt>
1012     * is not supported by this iterator.
1013 tim 1.1 */
1014 jsr166 1.33 public void set(E e) {
1015 tim 1.1 throw new UnsupportedOperationException();
1016     }
1017    
1018     /**
1019     * Not supported. Always throws UnsupportedOperationException.
1020 jsr166 1.32 * @throws UnsupportedOperationException always; <tt>add</tt>
1021     * is not supported by this iterator.
1022 tim 1.1 */
1023 jsr166 1.33 public void add(E e) {
1024 tim 1.1 throw new UnsupportedOperationException();
1025     }
1026     }
1027    
1028     /**
1029 dl 1.40 * Returns a view of the portion of this list between
1030     * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive.
1031     * The returned list is backed by this list, so changes in the
1032     * returned list are reflected in this list, and vice-versa.
1033     * While mutative operations are supported, they are probably not
1034     * very useful for CopyOnWriteArrayLists.
1035     *
1036     * <p>The semantics of the list returned by this method become
1037     * undefined if the backing list (i.e., this list) is
1038     * <i>structurally modified</i> in any way other than via the
1039     * returned list. (Structural modifications are those that change
1040     * the size of the list, or otherwise perturb it in such a fashion
1041     * that iterations in progress may yield incorrect results.)
1042 tim 1.1 *
1043 jsr166 1.35 * @param fromIndex low endpoint (inclusive) of the subList
1044     * @param toIndex high endpoint (exclusive) of the subList
1045     * @return a view of the specified range within this list
1046     * @throws IndexOutOfBoundsException {@inheritDoc}
1047 tim 1.1 */
1048 dl 1.42 public List<E> subList(int fromIndex, int toIndex) {
1049 jsr166 1.45 final ReentrantLock lock = this.lock;
1050     lock.lock();
1051     try {
1052     Object[] elements = getArray();
1053     int len = elements.length;
1054     if (fromIndex < 0 || toIndex > len || fromIndex > toIndex)
1055     throw new IndexOutOfBoundsException();
1056     return new COWSubList<E>(this, fromIndex, toIndex);
1057     } finally {
1058     lock.unlock();
1059     }
1060 tim 1.1 }
1061    
1062 dl 1.42 /**
1063     * Sublist for CopyOnWriteArrayList.
1064     * This class extends AbstractList merely for convenience, to
1065     * avoid having to define addAll, etc. This doesn't hurt, but
1066     * is wasteful. This class does not need or use modCount
1067     * mechanics in AbstractList, but does need to check for
1068     * concurrent modification using similar mechanics. On each
1069     * operation, the array that we expect the backing list to use
1070     * is checked and updated. Since we do this for all of the
1071     * base operations invoked by those defined in AbstractList,
1072     * all is well. While inefficient, this is not worth
1073     * improving. The kinds of list operations inherited from
1074     * AbstractList are already so slow on COW sublists that
1075     * adding a bit more space/time doesn't seem even noticeable.
1076     */
1077 tim 1.1 private static class COWSubList<E> extends AbstractList<E> {
1078     private final CopyOnWriteArrayList<E> l;
1079     private final int offset;
1080     private int size;
1081 dl 1.41 private Object[] expectedArray;
1082 tim 1.1
1083 jsr166 1.45 // only call this holding l's lock
1084 tim 1.1 private COWSubList(CopyOnWriteArrayList<E> list,
1085 jsr166 1.45 int fromIndex, int toIndex) {
1086 tim 1.1 l = list;
1087 dl 1.40 expectedArray = l.getArray();
1088 tim 1.1 offset = fromIndex;
1089     size = toIndex - fromIndex;
1090     }
1091    
1092     // only call this holding l's lock
1093     private void checkForComodification() {
1094 dl 1.40 if (l.getArray() != expectedArray)
1095 tim 1.1 throw new ConcurrentModificationException();
1096     }
1097    
1098     // only call this holding l's lock
1099     private void rangeCheck(int index) {
1100 jsr166 1.45 if (index<0 || index>=size)
1101     throw new IndexOutOfBoundsException("Index: "+index+
1102     ",Size: "+size);
1103 tim 1.1 }
1104    
1105     public E set(int index, E element) {
1106 jsr166 1.45 final ReentrantLock lock = l.lock;
1107     lock.lock();
1108     try {
1109 tim 1.1 rangeCheck(index);
1110     checkForComodification();
1111 jsr166 1.45 E x = l.set(index+offset, element);
1112 dl 1.40 expectedArray = l.getArray();
1113 tim 1.1 return x;
1114 jsr166 1.45 } finally {
1115     lock.unlock();
1116     }
1117 tim 1.1 }
1118    
1119     public E get(int index) {
1120 jsr166 1.45 final ReentrantLock lock = l.lock;
1121     lock.lock();
1122     try {
1123 tim 1.1 rangeCheck(index);
1124     checkForComodification();
1125 jsr166 1.45 return l.get(index+offset);
1126     } finally {
1127     lock.unlock();
1128     }
1129 tim 1.1 }
1130    
1131     public int size() {
1132 jsr166 1.45 final ReentrantLock lock = l.lock;
1133     lock.lock();
1134     try {
1135 tim 1.1 checkForComodification();
1136     return size;
1137 jsr166 1.45 } finally {
1138     lock.unlock();
1139     }
1140 tim 1.1 }
1141    
1142     public void add(int index, E element) {
1143 jsr166 1.45 final ReentrantLock lock = l.lock;
1144     lock.lock();
1145     try {
1146 tim 1.1 checkForComodification();
1147     if (index<0 || index>size)
1148     throw new IndexOutOfBoundsException();
1149 jsr166 1.45 l.add(index+offset, element);
1150 dl 1.40 expectedArray = l.getArray();
1151 tim 1.1 size++;
1152 jsr166 1.45 } finally {
1153     lock.unlock();
1154     }
1155 tim 1.1 }
1156    
1157 dl 1.13 public void clear() {
1158 jsr166 1.45 final ReentrantLock lock = l.lock;
1159     lock.lock();
1160     try {
1161 dl 1.13 checkForComodification();
1162     l.removeRange(offset, offset+size);
1163 dl 1.40 expectedArray = l.getArray();
1164 dl 1.13 size = 0;
1165 jsr166 1.45 } finally {
1166     lock.unlock();
1167     }
1168 dl 1.13 }
1169    
1170 tim 1.1 public E remove(int index) {
1171 jsr166 1.45 final ReentrantLock lock = l.lock;
1172     lock.lock();
1173     try {
1174 tim 1.1 rangeCheck(index);
1175     checkForComodification();
1176 jsr166 1.45 E result = l.remove(index+offset);
1177 dl 1.40 expectedArray = l.getArray();
1178 tim 1.1 size--;
1179     return result;
1180 jsr166 1.45 } finally {
1181     lock.unlock();
1182     }
1183 tim 1.1 }
1184    
1185     public Iterator<E> iterator() {
1186 jsr166 1.45 final ReentrantLock lock = l.lock;
1187     lock.lock();
1188     try {
1189 tim 1.1 checkForComodification();
1190 tim 1.8 return new COWSubListIterator<E>(l, 0, offset, size);
1191 jsr166 1.45 } finally {
1192     lock.unlock();
1193     }
1194 tim 1.1 }
1195    
1196     public ListIterator<E> listIterator(final int index) {
1197 jsr166 1.45 final ReentrantLock lock = l.lock;
1198     lock.lock();
1199     try {
1200 tim 1.1 checkForComodification();
1201     if (index<0 || index>size)
1202 dl 1.40 throw new IndexOutOfBoundsException("Index: "+index+
1203 jsr166 1.45 ", Size: "+size);
1204 tim 1.8 return new COWSubListIterator<E>(l, index, offset, size);
1205 jsr166 1.45 } finally {
1206     lock.unlock();
1207     }
1208 tim 1.1 }
1209    
1210 tim 1.7 public List<E> subList(int fromIndex, int toIndex) {
1211 jsr166 1.45 final ReentrantLock lock = l.lock;
1212     lock.lock();
1213     try {
1214 tim 1.7 checkForComodification();
1215     if (fromIndex<0 || toIndex>size)
1216     throw new IndexOutOfBoundsException();
1217 dl 1.41 return new COWSubList<E>(l, fromIndex + offset,
1218 jsr166 1.45 toIndex + offset);
1219     } finally {
1220     lock.unlock();
1221     }
1222 tim 1.7 }
1223 tim 1.1
1224 tim 1.7 }
1225 tim 1.1
1226    
1227 tim 1.7 private static class COWSubListIterator<E> implements ListIterator<E> {
1228     private final ListIterator<E> i;
1229     private final int index;
1230     private final int offset;
1231     private final int size;
1232 dl 1.41 private COWSubListIterator(List<E> l, int index, int offset,
1233 jsr166 1.45 int size) {
1234 tim 1.7 this.index = index;
1235     this.offset = offset;
1236     this.size = size;
1237 jsr166 1.45 i = l.listIterator(index+offset);
1238 tim 1.7 }
1239 tim 1.1
1240 tim 1.7 public boolean hasNext() {
1241     return nextIndex() < size;
1242     }
1243 tim 1.1
1244 tim 1.7 public E next() {
1245     if (hasNext())
1246     return i.next();
1247     else
1248     throw new NoSuchElementException();
1249     }
1250 tim 1.1
1251 tim 1.7 public boolean hasPrevious() {
1252     return previousIndex() >= 0;
1253     }
1254 tim 1.1
1255 tim 1.7 public E previous() {
1256     if (hasPrevious())
1257     return i.previous();
1258     else
1259     throw new NoSuchElementException();
1260     }
1261 tim 1.1
1262 tim 1.7 public int nextIndex() {
1263     return i.nextIndex() - offset;
1264     }
1265 tim 1.1
1266 tim 1.7 public int previousIndex() {
1267     return i.previousIndex() - offset;
1268 tim 1.1 }
1269    
1270 tim 1.7 public void remove() {
1271     throw new UnsupportedOperationException();
1272     }
1273 tim 1.1
1274 jsr166 1.33 public void set(E e) {
1275 tim 1.7 throw new UnsupportedOperationException();
1276 tim 1.1 }
1277    
1278 jsr166 1.33 public void add(E e) {
1279 tim 1.7 throw new UnsupportedOperationException();
1280     }
1281 tim 1.1 }
1282    
1283 dl 1.42 // Support for resetting lock while deserializing
1284     private static final Unsafe unsafe = Unsafe.getUnsafe();
1285     private static final long lockOffset;
1286     static {
1287     try {
1288     lockOffset = unsafe.objectFieldOffset
1289     (CopyOnWriteArrayList.class.getDeclaredField("lock"));
1290     } catch (Exception ex) { throw new Error(ex); }
1291     }
1292     private void resetLock() {
1293     unsafe.putObjectVolatile(this, lockOffset, new ReentrantLock());
1294     }
1295    
1296 tim 1.1 }