Class LockIterator<T>

  • Type Parameters:
    T - The element type for this iterator
    All Implemented Interfaces:
    AutoCloseable, Iterator<T>, ClosableIterator<T>

    public class LockIterator<T>
    extends Object
    implements ClosableIterator<T>
    Simple implementation of a ClosableIterator that uses a lock.
    Close is an idempotent function and can be performed multiple times without effects beyond first invocation.
    This is deployed by CacheView.lockedIterator() to allow read-only access to the underlying structure without the need to clone it, unlike the normal iterator.

    This closes automatically when hasNext() returns false but its recommended to only be used within a try-with-resources block for safety.

    Example

    This can handle any exceptions thrown while iterating and ensures the lock is released correctly.
    
     try (ClosableIterator<T> it = cacheView.lockedIterator()) {
         while (it.hasNext()) {
             consume(it.next());
         }
     }
     
    Since:
    4.0.0