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:
-
Constructor Summary
Constructors -
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.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.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() -
DefaultAsyncChannel
public DefaultAsyncChannel(int capacity)
-
-
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. 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.- 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.- 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
-
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
-