Class ConcurrentList<E>

java.lang.Object
com.cedarsoftware.util.ConcurrentList<E>
Type Parameters:
E - The type of elements in this list
All Implemented Interfaces:
Iterable<E>, Collection<E>, List<E>

public class ConcurrentList<E> extends Object implements List<E>
A thread-safe implementation of the 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() and listIterator() 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

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: