Package org.apache.groovy.runtime.async

# Class GeneratorBridge<T>

java.lang.Object
org.apache.groovy.runtime.async.GeneratorBridge<T>

Type Parameters:
  `T` - the element type

All Implemented Interfaces:
  `Closeable`, `AutoCloseable`, `Iterator<T>`

public final class GeneratorBridge<T> extends Object implements Iterator<T>, Closeable
A producer/consumer bridge for async generators (`yield return`).

The generator closure runs on a separate thread and calls `yield(Object)` to produce values. The consumer iterates using `hasNext()`/`next()`. A `SynchronousQueue` provides the handoff — each `yield` blocks until the consumer takes the value, providing natural back-pressure.

With virtual threads (JDK 21+), both the producer and consumer block cheaply. On JDK 17-20, the producer runs on a platform thread from the cached pool.

Since:
  6.0.0

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `GeneratorBridge()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `close()`
  `boolean`
  `hasNext()`
  `T`
  `next()`
  `void`
  `yield(Object value)`
  Called by the generator (producer thread) to yield a value.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface java.util.Iterator

  `forEachRemaining, remove`

- ## Constructor Details

  - ### GeneratorBridge

    public GeneratorBridge()

- ## Method Details

  - ### yield

    public void yield(Object value)
    Called by the generator (producer thread) to yield a value. Blocks until the consumer calls `next()`.
    Parameters:
      `value` - the value to yield (may be null)
    Throws:
      `org.apache.groovy.runtime.async.GeneratorBridge.GeneratorClosedException` - if the consumer has closed the bridge

  - ### hasNext

    public boolean hasNext()
    Specified by:
      `hasNext` in interface `Iterator<T>`

  - ### next

    public T next()
    Specified by:
      `next` in interface `Iterator<T>`

  - ### close

    public void close()
    Specified by:
      `close` in interface `AutoCloseable`
    Specified by:
      `close` in interface `Closeable`