Class Channel<T>
- java.lang.Object
-
- de.esoco.coroutine.Channel<T>
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class Channel<T> extends java.lang.Object implements java.lang.AutoCloseableA channel that allows communication betweenCoroutines. 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 inCoroutineScopeandCoroutineContext. 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 withcreateChannel(ChannelId, int), but only if it doesn't exist already. Channels can be removed withremoveChannel(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 aChannelClosedException. Upon a close all pending suspensions will also be failed with that exception.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcheckClosed()Throws aChannelClosedExceptionif this channel is already closed.voidclose()Closes this channel.ChannelId<T>getId()Returns the channel identifier.booleanisClosed()Returns the closed.TreceiveBlocking()Receives a value from this channel, blocking if no data is available.voidreceiveSuspending(Suspension<T> suspension)Tries to receive a value from this channel and resumes the execution of aCoroutineat the given suspension as soon as a value becomes available.intremainingCapacity()Returns the number of values that can still be send to this channel.voidsendBlocking(T value)Sends a value into this channel, blocking if no capacity is available.voidsendSuspending(Suspension<T> suspension)Tries to send a value into this channel and resumes the execution of aCoroutineat the given step as soon as channel capacity becomes available.intsize()Returns the current number of values in this channel.java.lang.StringtoString()
-
-
-
Method Detail
-
checkClosed
public final void checkClosed()
Throws aChannelClosedExceptionif this channel is already closed.
-
close
public void close()
Closes this channel. All send and receive operations on a closed channel will throw aChannelClosedException. If there are remaining suspensions in this channel they will also be failed with such an exception.- Specified by:
closein interfacejava.lang.AutoCloseable
-
isClosed
public final boolean isClosed()
Returns the closed.- Returns:
- The closed
-
receiveBlocking
public T receiveBlocking()
Receives a value from this channel, blocking if no data is available.- Returns:
- The next value from this channel or NULL if the waiting for a value has been interrupted
-
receiveSuspending
public void receiveSuspending(Suspension<T> suspension)
Tries to receive a value from this channel and resumes the execution of aCoroutineat 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.- Parameters:
suspension- The coroutine suspension to resume after data has been receive
-
remainingCapacity
public int remainingCapacity()
Returns the number of values that can still be send to this channel. Due to the concurrent nature of channels this can only be a momentary value which needs to be interpreted with caution and necessary synchronization should be performed if applicable. Concurrently running coroutines could affect this value at any time.- Returns:
- The remaining channel capacity
-
sendBlocking
public void sendBlocking(T value)
Sends a value into this channel, blocking if no capacity is available.- Parameters:
value- The value to send
-
sendSuspending
public void sendSuspending(Suspension<T> suspension)
Tries to send a value into this channel and resumes the execution of aCoroutineat 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.- Parameters:
suspension- rValue The value to send
-
size
public int size()
Returns the current number of values in this channel. Due to the concurrent nature of channels this can only be a momentary value which needs to be interpreted with caution and necessary synchronization should be performed if applicable. Concurrently running coroutines could affect this value at any time.- Returns:
- The current number of channel entries
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-