Package org.apache.groovy.runtime.async
Class DefaultAsyncChannel<T>
java.lang.Object
org.apache.groovy.runtime.async.DefaultAsyncChannel<T>
- Type Parameters:
T- the payload type
- All Implemented Interfaces:
AsyncChannel<T>,Iterable<T>
Default lock-based implementation of
AsyncChannel.
Uses a ReentrantLock to coordinate access to the internal buffer
and the waiting-sender/waiting-receiver queues. All operations return
Awaitable immediately; the underlying CompletableFuture
is completed asynchronously when matching counterparts arrive.
- Since:
- 6.0.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanclose()Closes this channel.intReturns the number of values currently buffered.intReturns this channel's buffer capacity.booleanisClosed()Returnstrueif this channel has been closed.iterator()Returns a blocking iterator that receives values until the channel is closed and drained.receive()Receives the next value from this channel.Sends a value through this channel.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
DefaultAsyncChannel
public DefaultAsyncChannel() -
DefaultAsyncChannel
public DefaultAsyncChannel(int capacity)
-
-
Method Details
-
getCapacity
public int getCapacity()Description copied from interface:AsyncChannelReturns this channel's buffer capacity.- Specified by:
getCapacityin interfaceAsyncChannel<T>
-
getBufferedSize
public int getBufferedSize()Description copied from interface:AsyncChannelReturns the number of values currently buffered.- Specified by:
getBufferedSizein interfaceAsyncChannel<T>
-
isClosed
public boolean isClosed()Description copied from interface:AsyncChannelReturnstrueif this channel has been closed.- Specified by:
isClosedin interfaceAsyncChannel<T>
-
send
Description copied from interface:AsyncChannelSends a value through this channel.The returned
Awaitablecompletes when the value has been delivered to a receiver or buffered. Sending to a closed channel fails immediately withChannelClosedException.- Specified by:
sendin interfaceAsyncChannel<T>- Parameters:
value- the value to send; must not benull- Returns:
- an Awaitable that completes when the send succeeds
-
receive
Description copied from interface:AsyncChannelReceives the next value from this channel.The returned
Awaitablecompletes when a value is available. Receiving from a closed, empty channel fails withChannelClosedException.- Specified by:
receivein interfaceAsyncChannel<T>- Returns:
- an Awaitable that yields the next value
-
close
public boolean close()Description copied from interface:AsyncChannelCloses this channel. Idempotent.Buffered values remain receivable. Pending senders fail with
ChannelClosedException. After all buffered values are drained, subsequent receives also fail.- Specified by:
closein interfaceAsyncChannel<T>- Returns:
trueif this call actually closed the channel
-
iterator
Returns a blocking iterator that receives values until the channel is closed and drained. Eachnext()call blocks until a value is available.ChannelClosedExceptionsignals end-of-iteration. -
toString
-