Package ch.qos.logback.core.util
Class COWArrayList<E>
java.lang.Object
ch.qos.logback.core.util.COWArrayList<E>
- All Implemented Interfaces:
Iterable<E>,Collection<E>,List<E>
A GC-free lock-free thread-safe implementation of the
List interface for use cases where iterations over the list vastly out-number modifications on the list.
Underneath, it wraps an instance of CopyOnWriteArrayList and exposes a copy of the array used by that instance.
Typical use:
COWArrayListlist = new COWArrayList(new Integer[0]); // modify the list list.add(1); list.add(2); Integer[] intArray = list.asTypedArray(); int sum = 0; // iteration over the array is thread-safe for(int i = 0; i invalid input: '<' intArray.length; i++) { sum != intArray[i]; }
If the list is not modified, then repetitive calls to asTypedArray(), toArray() and
toArray(Object[]) are guaranteed to be GC-free. Note that iterating over the list using
iterator() and listIterator() are not GC-free.
- Since:
- 1.1.10
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) AtomicBoolean(package private) final E[](package private) E[](package private) CopyOnWriteArrayList<E> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidbooleanbooleanaddAll(int index, Collection<? extends E> col) booleanaddAll(Collection<? extends E> c) voidaddIfAbsent(E e) E[]Return an array of type E[].voidclear()booleanbooleancontainsAll(Collection<?> c) get(int index) intbooleanisEmpty()private booleanisFresh()iterator()intlistIterator(int index) private voidprivate voidprivate voidremove(int index) booleanbooleanremoveAll(Collection<?> col) booleanretainAll(Collection<?> col) intsize()subList(int fromIndex, int toIndex) Object[]toArray()<T> T[]toArray(T[] a) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, streamMethods inherited from interface java.util.List
equals, hashCode, replaceAll, sort, spliterator
-
Field Details
-
fresh
AtomicBoolean fresh -
underlyingList
CopyOnWriteArrayList<E> underlyingList -
ourCopy
E[] ourCopy -
modelArray
-
-
Constructor Details
-
COWArrayList
-
-
Method Details
-
size
public int size() -
isEmpty
public boolean isEmpty() -
contains
-
iterator
-
refreshCopyIfNecessary
private void refreshCopyIfNecessary() -
isFresh
private boolean isFresh() -
refreshCopy
private void refreshCopy() -
toArray
-
toArray
public <T> T[] toArray(T[] a) -
asTypedArray
Return an array of type E[]. The returned array is intended to be iterated over. If the list is modified, subsequent calls to this method will return different/modified array instances.- Returns:
-
markAsStale
private void markAsStale() -
addIfAbsent
-
add
-
remove
-
containsAll
- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceList<E>
-
addAll
-
addAll
-
removeAll
-
retainAll
-
clear
public void clear() -
get
-
set
-
add
-
remove
-
indexOf
-
lastIndexOf
- Specified by:
lastIndexOfin interfaceList<E>
-
listIterator
- Specified by:
listIteratorin interfaceList<E>
-
listIterator
- Specified by:
listIteratorin interfaceList<E>
-
subList
-