Class ConcurrentList<E>
- Type Parameters:
E
- The type of elements in this list
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,List<E>
List
interface, designed for use in highly concurrent environments.
The ConcurrentList
can be used either as a standalone thread-safe list or as a wrapper to make an existing
list thread-safe. It ensures thread safety without duplicating elements, making it suitable for applications
requiring synchronized access to list data.
Features
- Standalone Mode: Use the no-argument constructor to create a new thread-safe
ConcurrentList
. - Wrapper Mode: Pass an existing
List
to the constructor to wrap it with thread-safe behavior. - Read-Only Iterators: The
iterator()
andlistIterator()
methods return a read-only snapshot of the list at the time of the call, ensuring safe iteration in concurrent environments. - Unsupported Operations: Due to the dynamic nature of concurrent edits, the following operations are
not implemented:
listIterator(int)
: The starting index may no longer be valid due to concurrent modifications.subList(int, int)
: The range may exceed the current list size in a concurrent context.
Thread Safety
All public methods of ConcurrentList
are thread-safe, ensuring that modifications and access
operations can safely occur concurrently. However, thread safety depends on the correctness of the provided
backing list in wrapper mode.
Usage
// Standalone thread-safe list
ConcurrentList<String> standaloneList = new ConcurrentList<>();
standaloneList.add("Hello");
standaloneList.add("World");
// Wrapping an existing list
List<String> existingList = new ArrayList<>();
existingList.add("Java");
existingList.add("Concurrency");
ConcurrentList<String> wrappedList = new ConcurrentList<>(existingList);
Performance Considerations
The iterator()
and listIterator()
methods return read-only views created by copying
the list contents, which ensures thread safety but may incur a performance cost for very large lists.
Modifications to the list during iteration will not be reflected in the iterators.
Additional Notes
ConcurrentList
supportsnull
elements if the underlying list does.listIterator(int)
andsubList(int, int)
throwUnsupportedOperationException
.
- Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
License
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. - See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionNo-arg constructor to create an empty ConcurrentList, wrapping an ArrayList.ConcurrentList
(int size) Initial capacity supportConcurrentList
(List<E> list) Use this constructor to wrap a List (any kind of List) and make it a ConcurrentList. -
Method Summary
Modifier and TypeMethodDescriptionvoid
boolean
boolean
addAll
(int index, Collection<? extends E> c) boolean
addAll
(Collection<? extends E> c) void
clear()
boolean
boolean
containsAll
(Collection<?> c) boolean
get
(int index) int
hashCode()
int
boolean
isEmpty()
iterator()
int
listIterator
(int index) remove
(int index) boolean
boolean
removeAll
(Collection<?> c) boolean
retainAll
(Collection<?> c) int
size()
subList
(int fromIndex, int toIndex) Object[]
toArray()
<T> T[]
toArray
(T[] a) toString()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream
Methods inherited from interface java.util.List
replaceAll, sort, spliterator
-
Constructor Details
-
ConcurrentList
public ConcurrentList()No-arg constructor to create an empty ConcurrentList, wrapping an ArrayList. -
ConcurrentList
public ConcurrentList(int size) Initial capacity support -
ConcurrentList
Use this constructor to wrap a List (any kind of List) and make it a ConcurrentList. No duplicate of the List is created and the original list is operated on directly.- Parameters:
list
- List instance to protect.
-
-
Method Details
-
equals
-
hashCode
public int hashCode() -
toString
-
size
public int size() -
isEmpty
public boolean isEmpty() -
contains
-
containsAll
- Specified by:
containsAll
in interfaceCollection<E>
- Specified by:
containsAll
in interfaceList<E>
-
get
-
indexOf
-
lastIndexOf
- Specified by:
lastIndexOf
in interfaceList<E>
-
iterator
-
toArray
-
toArray
public <T> T[] toArray(T[] a) -
add
-
addAll
-
addAll
-
add
-
set
-
remove
-
remove
-
removeAll
-
retainAll
-
clear
public void clear() -
listIterator
- Specified by:
listIterator
in interfaceList<E>
-
listIterator
- Specified by:
listIterator
in interfaceList<E>
-
subList
-