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>
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 Details
-
GeneratorBridge
public GeneratorBridge()
-
-
Method Details
-
yield
Called by the generator (producer thread) to yield a value. Blocks until the consumer callsnext().- 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() -
next
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-