src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java
Print this page
rev 6969 : [mq]: iterator
@@ -34,10 +34,11 @@
*/
package java.util.concurrent;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
+import java.util.function.Consumer;
/**
* A thread-safe variant of {@link java.util.ArrayList} in which all mutative
* operations (<tt>add</tt>, <tt>set</tt>, and so on) are implemented by
* making a fresh copy of the underlying array.
@@ -1054,10 +1055,20 @@
* is not supported by this iterator.
*/
public void add(E e) {
throw new UnsupportedOperationException();
}
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public void forEachRemaining(Consumer<? super E> action) {
+ Objects.requireNonNull(action);
+ final int size = snapshot.length;
+ for (int i=cursor; i < size; i++) {
+ action.accept((E) snapshot[i]);
+ }
+ }
}
/**
* Returns a view of the portion of this list between
* <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive.
@@ -1313,10 +1324,19 @@
}
public void add(E e) {
throw new UnsupportedOperationException();
}
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public void forEachRemaining(Consumer<? super E> action) {
+ Objects.requireNonNull(action);
+ while (nextIndex() < size) {
+ action.accept(it.next());
+ }
+ }
}
// Support for resetting lock while deserializing
private void resetLock() {
UNSAFE.putObjectVolatile(this, lockOffset, new ReentrantLock());