public class Channel<T>
extends java.lang.Object
implements java.lang.AutoCloseable
Coroutines
. A
channel has a fixed capacity and suspends any further sending of data after
the capacity has been reached until capacity becomes available again when
data is requested by receivers. Receiving will be suspended if no more data
is available in a channel.
Channels can be queried with the method getChannel(ChannelId)
which is
available in CoroutineScope
and CoroutineContext
. If no
channel exists at first access a new channel with a capacity of 1 will be
created. A channel with a different capacity can be created with createChannel(ChannelId,
int)
, but only if it doesn't exist already. Channels can be removed with
removeChannel(ChannelId)
.
If a channel is needed for the communication between coroutines in different scopes it needs to be created in a common context of the scopes. If it is only needed in a particular scope it should be created there.
Channels can be closed by invoking close()
. A closed channel
rejects any further send or receive calls by throwing a ChannelClosedException
. Upon a close all pending suspensions will also be
failed with that exception.
Modifier | Constructor and Description |
---|---|
protected |
Channel(ChannelId<T> rId,
int nCapacity)
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
void |
checkClosed()
Throws a
ChannelClosedException if this channel is already
closed. |
void |
close()
Closes this channel.
|
ChannelId<T> |
getId()
Returns the channel identifier.
|
boolean |
isClosed()
Returns the closed.
|
T |
receiveBlocking()
Receives a value from this channel, blocking if no data is available.
|
void |
receiveSuspending(Suspension<T> rSuspension)
Tries to receive a value from this channel and resumes the execution of a
Coroutine at the given suspension as soon as a value becomes
available. |
int |
remainingCapacity()
Returns the number of values that can still be send to this channel.
|
void |
sendBlocking(T rValue)
Sends a value into this channel, blocking if no capacity is available.
|
void |
sendSuspending(Suspension<T> rSuspension)
Tries to send a value into this channel and resumes the execution of a
Coroutine at the given step as soon as channel capacity becomes
available. |
int |
size()
Returns the current number of values in this channel.
|
java.lang.String |
toString() |
public final void checkClosed()
ChannelClosedException
if this channel is already
closed.public void close()
ChannelClosedException
. If there are remaining
suspensions in this channel they will also be failed with such an
exception.close
in interface java.lang.AutoCloseable
public final boolean isClosed()
public T receiveBlocking()
public void receiveSuspending(Suspension<T> rSuspension)
Coroutine
at the given suspension as soon as a value becomes
available. This can be immediately or, if the channel is empty, only
after some other code sends a values into this channel. Suspended senders
will be served with a first-suspended-first-served policy.rSuspension
- The coroutine suspension to resume after data has been
receivepublic int remainingCapacity()
public void sendBlocking(T rValue)
rValue
- The value to sendpublic void sendSuspending(Suspension<T> rSuspension)
Coroutine
at the given step as soon as channel capacity becomes
available. This can be immediately or, if the channel is full, only after
some other code receives a values from this channel. Suspended senders
will be served with a first-suspended-first-served policy.rSuspension
- rValue The value to sendpublic int size()
public java.lang.String toString()
toString
in class java.lang.Object