Interacting with channels
The behavior of Channel<T>
is composed by two interfaces, SendChannel<T>
and ReceiveChannel<T>
. In this section, we will take a look at the functions defined by each and how they are used to interact with a channel.
SendChannel
This interface defines a couple of functions to send elements through a channel and other functions in order to validate that it's possible to send something.
Validating before sending
There are a few validations that you can perform before trying to send elements through the channel. The most common one is to validate that the channel has not been closed for sending. To do this, you can use isClosedForSend
:
val channel = Channel<Int>() channel.isClosedForSend // false channel.close() channel.isClosedForSend // true
You can also check whether the channel is out of capacity. When a channel is full, it will suspend the next time you call send
, so this property is useful if you would prefer not to suspend your coroutine at...