SendChannel

interface SendChannel<in E> (source)

Sender’s interface to Channel.

Properties

isClosedForSend

abstract val isClosedForSend: Boolean

Returns true if this channel was closed by an invocation of close. This means that calling send or offer will result in an exception.

onSend

abstract val onSend: SelectClause2<E, SendChannel<E>>

Clause for the select expression of the send suspending function that selects when the element that is specified as the parameter is sent to the channel. When the clause is selected, the reference to this channel is passed into the corresponding block.

Functions

close

abstract fun close(cause: Throwable? = null): Boolean

Closes this channel. This is an idempotent operation — subsequent invocations of this function have no effect and return false. Conceptually, it sends a special “close token” over this channel.

invokeOnClose

abstract fun invokeOnClose(
    handler: (cause: Throwable?) -> Unit
): Unit

Registers a handler which is synchronously invoked once the channel is closed or the receiving side of this channel is cancelled. Only one handler can be attached to a channel during its lifetime. The handler is invoked when isClosedForSend starts to return true. If the channel is closed already, the handler is invoked immediately.

offer

abstract fun offer(element: E): Boolean

Immediately adds the specified element to this channel, if this doesn’t violate its capacity restrictions, and returns true. Otherwise, just returns false. This is a synchronous variant of send which backs off in situations when send suspends.

send

abstract suspend fun send(element: E): Unit

Sends the specified element to this channel, suspending the caller while the buffer of this channel is full or if it does not exist, or throws an exception if the channel is closed for send (see close for details).

Extension Functions

sendBlocking

fun <E> SendChannel<E>.sendBlocking(element: E): Unit

Adds element into to this channel, blocking the caller while this channel Channel.isFull, or throws exception if the channel Channel.isClosedForSend (see Channel.close for details).

Inheritors

BroadcastChannel

interface BroadcastChannel<E> : SendChannel<E>

Broadcast channel is a non-blocking primitive for communication between the sender and multiple receivers that subscribe for the elements using openSubscription function and unsubscribe using ReceiveChannel.cancel function.

Channel

interface Channel<E> : SendChannel<E>, ReceiveChannel<E>

Channel is a non-blocking primitive for communication between a sender (via SendChannel) and a receiver (via ReceiveChannel). Conceptually, a channel is similar to Java’s BlockingQueue, but it has suspending operations instead of blocking ones and can be closed.

ProducerScope

interface ProducerScope<in E> : 
    CoroutineScope,
    SendChannel<E>

Scope for the produce coroutine builder.