Class DefaultAsyncChannel<T>
- Type Parameters:
T- the payload type
- All Implemented Interfaces:
AsyncChannel<T>,Iterable<T>
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.
Every operation is arbitrated by a SelectClaim: the branches of a
ChannelSelect share their select's claim, and a
plain operation carries a private one. The claim is the sole owner of a
parked operation's fate — a delivery commits it before it completes the
future, and cancellation must commit it before it may touch the future —
so a select over sends and receives on several channels commits exactly
one transfer, and a losing branch never disturbs its channel.
Both waiting queues are concurrent deques so that a cancelled operation
can withdraw itself without taking the channel lock: a
ChannelSelect withdraws its losing branches from
inside the winning channel's delivery, and taking a second channel's lock
there could deadlock against a select completing on that channel.
- Since:
- 6.0.0
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface groovy.concurrent.AsyncChannel
AsyncChannel.Producer<T> -
Constructor Summary
ConstructorsConstructorDescriptionCreates a channel that does not buffer.DefaultAsyncChannel(int capacity) Creates a channel that buffers at mostcapacityvalues. -
Method Summary
Modifier and TypeMethodDescriptionstatic DefaultAsyncChannel<Instant>Creates a timer channel: a capacity-1 channel that delivers one value, theInstantat which it fired, oncedelayhas elapsed from this call, and then closes.booleanclose()Closes this channel.booleanRetainscauseand closes.intReturns the number of values currently buffered.intReturns this channel's buffer capacity.Returns the failure passed toAsyncChannel.close(Throwable), ornullwhen this channel is open, was closed normally, or does not retain a cause.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.receiveIfUnclaimed(SelectClaim claim) Receives the next value, but only ifclaimcommits to this branch at the moment this channel would hand the value over.Sends a value through this channel.sendIfUnclaimed(T value, SelectClaim claim) Offersvalue, but only ifclaimcommits to this branch at the moment the channel could accept it: when a waiting receiver takes it, or when buffer space holds it.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()Creates a channel that does not buffer. A send completes immediately only when a receiver is already waiting; otherwise the send stays queued. -
DefaultAsyncChannel
public DefaultAsyncChannel(int capacity) Creates a channel that buffers at mostcapacityvalues. Zero does not buffer: a send stays queued unless a receiver is already waiting. A negative capacity is rejected.- Parameters:
capacity- maximum number of buffered values
-
-
Method Details
-
after
Creates a timer channel: a capacity-1 channel that delivers one value, theInstantat which it fired, oncedelayhas elapsed from this call, and then closes. Firing buffers the instant and closes in one critical section, so a waiter that takes the value observesisClosed()as true rather than racing a laterclose(). Closing the channel before it fires cancels the timer, so a timer that is no longer wanted holds no scheduler slot. A delay that is not positive has already elapsed, so the channel is returned already holding its instant and closed: it is ready to a receiver or a select at once, with no scheduler hop.Implementation of
AsyncChannel.after(long); not part of the channel contract.- Parameters:
delay- how long to wait before firingunit- the unit ofdelay- Returns:
- the timer channel
-
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. When the channel was closed with a failure, that failure is the exception's cause.- Specified by:
sendin interfaceAsyncChannel<T>- Parameters:
value- the value to send; must not benull- Returns:
- an Awaitable that completes when the send succeeds
-
sendIfUnclaimed
Offersvalue, but only ifclaimcommits to this branch at the moment the channel could accept it: when a waiting receiver takes it, or when buffer space holds it. A group of offers sharing one claim (the branches of aChannelSelect) commits exactly one between them; an offer whose claim was committed elsewhere is retired without any effect on the channel — no buffered residue, no lingering waiting sender.Internal support for
ChannelSelect; not part of the channel contract.- Parameters:
value- the value offered for transferclaim- the claim shared by the competing offers- Returns:
- an Awaitable that completes when the send committed, or is cancelled if the claim was taken elsewhere first
-
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. When the channel was closed with a failure, that failure is the exception's cause. A loop that catches onlyChannelClosedExceptiondrops the failure unless it readsThrowable.getCause().for awaitthrows the failure itself; the code that reads the channel writes thetry/catch.- Specified by:
receivein interfaceAsyncChannel<T>- Returns:
- an Awaitable that yields the next value
-
receiveIfUnclaimed
Receives the next value, but only ifclaimcommits to this branch at the moment this channel would hand the value over. The claim is resolved under the channel lock immediately before the value is dequeued, so a group of offers sharing one claim (the branches of aChannelSelect) takes exactly one value between them: a branch that loses the claim never touches its channel's contents and is completed as cancelled.Internal support for
ChannelSelect; not part of the channel contract.- Parameters:
claim- the claim shared by the competing offers- Returns:
- an Awaitable that yields the next value, or is cancelled if the claim was taken elsewhere first
-
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- See Also:
-
getCloseCause
Description copied from interface:AsyncChannelReturns the failure passed toAsyncChannel.close(Throwable), ornullwhen this channel is open, was closed normally, or does not retain a cause.The default is
null.receiveandsenddo not throw this failure; it is the cause of theChannelClosedExceptionthey throw.- Specified by:
getCloseCausein interfaceAsyncChannel<T>- Returns:
- the retained failure, or
null
-
close
Retainscauseand closes. Does not call the interface default.- Specified by:
closein interfaceAsyncChannel<T>- Parameters:
cause- the failure iteration should throw, ornullfor a normal close- Returns:
trueif this call 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. AChannelClosedExceptionwith no cause ends iteration. A failure retained byclose(Throwable)is that exception's cause and is thrown here, once, for the caller to catch.for,each,collectandtoListall use this iterator. A Java enhancedforcallshasNext(), which may throw a checked exception thatIteratordoes not declare. -
toString
-