Channel
interface Channel<E> : SendChannel<E>, ReceiveChannel<E>
(source)Channel is a nonblocking primitive for communication between sender using SendChannel and receiver using ReceiveChannel. Conceptually, a channel is similar to BlockingQueue, but it has suspending operations instead of blocking ones and it can be closed.
Channel(capacity)
factory function is used to create channels of different kind depending on
the value of capacity
integer:

When
capacity
is 0 – it createsRendezvousChannel
. This channel does not have any buffer at all. An element is transferred from sender to receiver only when send and receive invocations meet in time (rendezvous), so send suspends until another coroutine invokes receive and receive suspends until another coroutine invokes send. 
When
capacity
is Channel.UNLIMITED – it createsLinkedListChannel
. This is a channel with linkedlist buffer of a unlimited capacity (limited only by available memory). Sender to this channel never suspends and offer always returnstrue
. 
When
capacity
is Channel.CONFLATED – it createsConflatedChannel
. This channel buffers at most one element and conflates all subsequentsend
andoffer
invocations, so that the receiver always gets the most recently sent element. Backtosend sent elements are conflated – only the the most recently sent element is received, while previously sent elements are lost. Sender to this channel never suspends and offer always returnstrue
. 
When
capacity
is positive, but less than UNLIMITED – it creates arraybased channel with given capacity. This channel has an array buffer of a fixedcapacity
. Sender suspends only when buffer is fully and receiver suspends only when buffer is empty.
Inherited Properties
abstract val isClosedForReceive: Boolean Returns 

abstract val isClosedForSend: Boolean Returns 

abstract val isEmpty: Boolean Returns 

abstract val isFull: Boolean Returns 

abstract val onReceive: SelectClause1<E> Clause for select expression of receive suspending function that selects with the element that is received from the channel. The select invocation fails with exception if the channel isClosedForReceive (see close for details). 

abstract val onReceiveOrNull: SelectClause1<E?> Clause for select expression of receiveOrNull suspending function that selects with the element that
is received from the channel or selects with 

abstract val onSend: SelectClause2<E, SendChannel<E>> Clause for select expression of send suspending function that selects when the element that is specified as parameter is sent to the channel. When the clause is selected the reference to this channel is passed into the corresponding block. 
Inherited Functions
abstract fun cancel(): Unit Cancels reception of remaining elements from this channel. This function closes the channel and removes all buffered sent elements from it. 

Closes this channel.
This is an idempotent operation – repeated invocations of this function have no effect and return 

Registers handler which is synchronously invoked once the channel is closed
or receiving side of this channel is cancelled.
Only one handler can be attached to the channel during channel’s lifetime.
Handler is invoked when isClosedForSend starts to return 

abstract operator fun iterator(): ChannelIterator<E> Returns new iterator to receive elements from this channels using 

Adds element into this queue if it is possible to do so immediately without violating capacity restrictions
and returns 

abstract fun poll(): E? Retrieves and removes the element from this channel, or returns 

abstract suspend fun receive(): E Retrieves and removes the element from this channel suspending the caller while this channel isEmpty or throws ClosedReceiveChannelException if the channel isClosedForReceive. If the channel was closed because of the exception, it is called a failed channel and this function throws the original close cause exception. 

abstract suspend fun receiveOrNull(): E? Retrieves and removes the element from this channel suspending the caller while this channel isEmpty
or returns 

Adds element into to this channel, suspending the caller while this channel isFull, or throws exception if the channel isClosedForSend (see close for details). 
Companion Object Properties
const val CONFLATED: Int Requests conflated channel in 

const val RENDEZVOUS: Int Requests rendezvous channel in 

const val UNLIMITED: Int Requests channel with unlimited capacity buffer in 
Extension Functions
suspend fun <E> ReceiveChannel<E>.all( Returns 

suspend fun <E> ReceiveChannel<E>.any(): Boolean Returns suspend fun <E> ReceiveChannel<E>.any( Returns 

Returns a Map containing keyvalue pairs provided by transform function applied to elements of the given channel. 

Returns a Map containing the elements from the given channel indexed by the key returned from keySelector function applied to each element. suspend fun <E, K, V> ReceiveChannel<E>.associateBy( Returns a Map containing the values provided by valueTransform and indexed by keySelector functions applied to elements of the given channel. 

suspend fun <E, K, M : MutableMap<in K, in E>> ReceiveChannel<E>.associateByTo( Populates and returns the destination mutable map with keyvalue pairs, where key is provided by the keySelector function applied to each element of the given channel and value is the element itself. suspend fun <E, K, V, M : MutableMap<in K, in V>> ReceiveChannel<E>.associateByTo( Populates and returns the destination mutable map with keyvalue pairs, where key is provided by the keySelector function and and value is provided by the valueTransform function applied to elements of the given channel. 

suspend fun <E, K, V, M : MutableMap<in K, in V>> ReceiveChannel<E>.associateTo( Populates and returns the destination mutable map with keyvalue pairs provided by transform function applied to each element of the given channel. 

fun <E> ReceiveChannel<E>.broadcast( Broadcasts all elements of the channel. 

fun <E, R> ReceiveChannel<E>.consume( Makes sure that the given block consumes all elements from the given channel by always invoking cancel after the execution of the block. 

suspend fun <E> ReceiveChannel<E>.consumeEach( Performs the given action for each received element. 

suspend fun <E> ReceiveChannel<E>.consumeEachIndexed( Performs the given action for each received element. 

fun ReceiveChannel<*>.consumes(): CompletionHandler Returns a CompletionHandler that invokes cancel on the ReceiveChannel with the corresponding cause. See also ReceiveChannel.consume. 

suspend fun <E> ReceiveChannel<E>.count(): Int Returns the number of elements in this channel. suspend fun <E> ReceiveChannel<E>.count( Returns the number of elements matching the given predicate. 

fun <E> ReceiveChannel<E>.distinct(): ReceiveChannel<E> Returns a channel containing only distinct elements from the given channel. 

fun <E, K> ReceiveChannel<E>.distinctBy( Returns a channel containing only elements from the given channel having distinct keys returned by the given selector function. 

fun <E> ReceiveChannel<E>.drop( Returns a channel containing all elements except first n elements. 

fun <E> ReceiveChannel<E>.dropWhile( Returns a channel containing all elements except first elements that satisfy the given predicate. 

suspend fun <E> ReceiveChannel<E>.elementAt(index: Int): E Returns an element at the given index or throws an IndexOutOfBoundsException if the index is out of bounds of this channel. 

Returns an element at the given index or the result of calling the defaultValue function if the index is out of bounds of this channel. 

suspend fun <E> ReceiveChannel<E>.elementAtOrNull( Returns an element at the given index or 

fun <E> ReceiveChannel<E>.filter( Returns a channel containing only elements matching the given predicate. 

fun <E> ReceiveChannel<E>.filterIndexed( Returns a channel containing only elements matching the given predicate. 

suspend fun <E, C : MutableCollection<in E>> ReceiveChannel<E>.filterIndexedTo( Appends all elements matching the given predicate to the given destination. 

fun <E> ReceiveChannel<E>.filterNot( Returns a channel containing all elements not matching the given predicate. 

fun <E : Any> ReceiveChannel<E?>.filterNotNull(): ReceiveChannel<E> Returns a channel containing all elements that are not 

suspend fun <E : Any, C : MutableCollection<in E>> ReceiveChannel<E?>.filterNotNullTo( Appends all elements that are not 

suspend fun <E, C : MutableCollection<in E>> ReceiveChannel<E>.filterNotTo( Appends all elements not matching the given predicate to the given destination. 

suspend fun <E, C : MutableCollection<in E>> ReceiveChannel<E>.filterTo( Appends all elements matching the given predicate to the given destination. 

suspend fun <E> ReceiveChannel<E>.find( Returns the first element matching the given predicate, or 

suspend fun <E> ReceiveChannel<E>.findLast( Returns the last element matching the given predicate, or 

suspend fun <E> ReceiveChannel<E>.first(): E Returns first element. suspend fun <E> ReceiveChannel<E>.first( Returns the first element matching the given predicate. 

suspend fun <E> ReceiveChannel<E>.firstOrNull(): E? Returns the first element, or suspend fun <E> ReceiveChannel<E>.firstOrNull( Returns the first element matching the given predicate, or 

fun <E, R> ReceiveChannel<E>.flatMap( Returns a single channel of all elements from results of transform function being invoked on each element of original channel. 

Accumulates value starting with initial value and applying operation from left to right to current accumulator value and each element. 

suspend fun <E, R> ReceiveChannel<E>.foldIndexed( Accumulates value starting with initial value and applying operation from left to right to current accumulator value and each element with its index in the original channel. 

Groups elements of the original channel by the key returned by the given keySelector function applied to each element and returns a map where each group key is associated with a list of corresponding elements. suspend fun <E, K, V> ReceiveChannel<E>.groupBy( Groups values returned by the valueTransform function applied to each element of the original channel by the key returned by the given keySelector function applied to the element and returns a map where each group key is associated with a list of corresponding values. 

suspend fun <E, K, M : MutableMap<in K, MutableList<E>>> ReceiveChannel<E>.groupByTo( Groups elements of the original channel by the key returned by the given keySelector function applied to each element and puts to the destination map each group key associated with a list of corresponding elements. suspend fun <E, K, V, M : MutableMap<in K, MutableList<V>>> ReceiveChannel<E>.groupByTo( Groups values returned by the valueTransform function applied to each element of the original channel by the key returned by the given keySelector function applied to the element and puts to the destination map each group key associated with a list of corresponding values. 

suspend fun <E> ReceiveChannel<E>.indexOf(element: E): Int Returns first index of element, or 1 if the channel does not contain element. 

suspend fun <E> ReceiveChannel<E>.indexOfFirst( Returns index of the first element matching the given predicate, or 1 if the channel does not contain such element. 

suspend fun <E> ReceiveChannel<E>.indexOfLast( Returns index of the last element matching the given predicate, or 1 if the channel does not contain such element. 

suspend fun <E> ReceiveChannel<E>.last(): E Returns the last element. suspend fun <E> ReceiveChannel<E>.last( Returns the last element matching the given predicate. 

suspend fun <E> ReceiveChannel<E>.lastIndexOf( Returns last index of element, or 1 if the channel does not contain element. 

suspend fun <E> ReceiveChannel<E>.lastOrNull(): E? Returns the last element, or suspend fun <E> ReceiveChannel<E>.lastOrNull( Returns the last element matching the given predicate, or 

fun <E, R> ReceiveChannel<E>.map( Returns a channel containing the results of applying the given transform function to each element in the original channel. 

fun <E, R> ReceiveChannel<E>.mapIndexed( Returns a channel containing the results of applying the given transform function to each element and its index in the original channel. 

fun <E, R : Any> ReceiveChannel<E>.mapIndexedNotNull( Returns a channel containing only the nonnull results of applying the given transform function to each element and its index in the original channel. 

suspend fun <E, R : Any, C : MutableCollection<in R>> ReceiveChannel<E>.mapIndexedNotNullTo( Applies the given transform function to each element and its index in the original channel and appends only the nonnull results to the given destination. 

suspend fun <E, R, C : MutableCollection<in R>> ReceiveChannel<E>.mapIndexedTo( Applies the given transform function to each element and its index in the original channel and appends the results to the given destination. 

fun <E, R : Any> ReceiveChannel<E>.mapNotNull( Returns a channel containing only the nonnull results of applying the given transform function to each element in the original channel. 

suspend fun <E, R : Any, C : MutableCollection<in R>> ReceiveChannel<E>.mapNotNullTo( Applies the given transform function to each element in the original channel and appends only the nonnull results to the given destination. 

suspend fun <E, R, C : MutableCollection<in R>> ReceiveChannel<E>.mapTo( Applies the given transform function to each element of the original channel and appends the results to the given destination. 

suspend fun <E, R : Comparable<R>> ReceiveChannel<E>.maxBy( Returns the first element yielding the largest value of the given function or 

suspend fun <E> ReceiveChannel<E>.maxWith( Returns the first element having the largest value according to the provided comparator or 

suspend fun <E, R : Comparable<R>> ReceiveChannel<E>.minBy( Returns the first element yielding the smallest value of the given function or 

suspend fun <E> ReceiveChannel<E>.minWith( Returns the first element having the smallest value according to the provided comparator or 

suspend fun <E> ReceiveChannel<E>.none(): Boolean Returns suspend fun <E> ReceiveChannel<E>.none( Returns 

Splits the original channel into pair of lists,
where first list contains elements for which predicate yielded 

Accumulates value starting with the first element and applying operation from left to right to current accumulator value and each element. 

Accumulates value starting with the first element and applying operation from left to right to current accumulator value and each element with its index in the original channel. 

fun <E : Any> ReceiveChannel<E?>.requireNoNulls(): ReceiveChannel<E> Returns an original collection containing all the non 

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). 

suspend fun <E> ReceiveChannel<E>.single(): E Returns the single element, or throws an exception if the channel is empty or has more than one element. suspend fun <E> ReceiveChannel<E>.single( Returns the single element matching the given predicate, or throws exception if there is no or more than one matching element. 

suspend fun <E> ReceiveChannel<E>.singleOrNull(): E? Returns single element, or suspend fun <E> ReceiveChannel<E>.singleOrNull( Returns the single element matching the given predicate, or 

suspend fun <E> ReceiveChannel<E>.sumBy( Returns the sum of all values produced by selector function applied to each element in the channel. 

suspend fun <E> ReceiveChannel<E>.sumByDouble( Returns the sum of all values produced by selector function applied to each element in the channel. 

fun <E> ReceiveChannel<E>.take( Returns a channel containing first n elements. 

fun <E> ReceiveChannel<E>.takeWhile( Returns a channel containing first elements satisfying the given predicate. 

suspend fun <E, C : SendChannel<E>> ReceiveChannel<E>.toChannel( Send each element of the original channel and appends the results to the given destination. 

suspend fun <E, C : MutableCollection<in E>> ReceiveChannel<E>.toCollection( Appends all elements to the given destination collection. 

suspend fun <E> ReceiveChannel<E>.toList(): List<E> Returns a List containing all elements. 

suspend fun <E> ReceiveChannel<E>.toMutableList(): MutableList<E> Returns a MutableList filled with all elements of this channel. 

suspend fun <E> ReceiveChannel<E>.toMutableSet(): MutableSet<E> Returns a mutable set containing all distinct elements from the given channel. 

suspend fun <E> ReceiveChannel<E>.toSet(): Set<E> Returns a Set of all elements. 

fun <E> ReceiveChannel<E>.withIndex( Returns a channel of IndexedValue for each element of the original channel. 

infix fun <E, R> ReceiveChannel<E>.zip( Returns a channel of pairs built from elements of both channels with same indexes. Resulting channel has length of shortest input channel. fun <E, R, V> ReceiveChannel<E>.zip( Returns a channel of values built from elements of both collections with same indexes using provided transform. Resulting channel has length of shortest input channels. 