Package org.bsc.async

Class AsyncGenerator.Base<E>

java.lang.Object
org.bsc.async.AsyncGenerator.Base<E>
Type Parameters:
E - the type of elements
All Implemented Interfaces:
AutoCloseable, Iterable<E>, AsyncGenerator<E>
Direct Known Subclasses:
AsyncGenerator.BaseCancellable
Enclosing interface:
AsyncGenerator<E>

public abstract static class AsyncGenerator.Base<E> extends Object implements AsyncGenerator<E>, AutoCloseable
Abstract base class for AsyncGenerator implementations.

This class manages an internal ExecutorService for asynchronous operations. It implements AutoCloseable to ensure proper resource cleanup.

Usage with try-with-resources:


 try (AsyncGenerator<String> generator = AsyncGenerator.from(list)) {
     generator.forEachAsync(System.out::println);
 }
 

Note: Even if close() is not called explicitly, the internal Cleaner mechanism will ensure the ExecutorService is properly shutdown when this instance is garbage collected. However, explicit cleanup via close() or cancel() is recommended for deterministic resource management.

  • Method Details

    • executor

      public Executor executor()
      Specified by:
      executor in interface AsyncGenerator<E>
    • close

      public void close()
      Closes this AsyncGenerator and releases its resources.

      This method shuts down the internal ExecutorService. After calling this method, the generator should not be used for further operations.

      This method is idempotent - calling it multiple times has no additional effect.

      Specified by:
      close in interface AutoCloseable
    • isClosed

      public boolean isClosed()
      Checks if this generator has been closed.
      Returns:
      true if the generator has been closed, false otherwise