public Channel<E> extends SendChannel<E>, ReceiveChannel<E>
Channel is a non-blocking primitive for communication between sender using interface SendChannel
and receiver using interface 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 creates RendezvousChannel
.
This channel does not have any buffer at all. An element is transferred from sender
to receiver only when SendChannel.send
and ReceiveChannel.receive
invocations meet in time (rendezvous), so SendChannel.send
suspends
until another coroutine invokes ReceiveChannel.receive
and ReceiveChannel.receive
suspends until another coroutine invokes SendChannel.send
.
When capacity
is Channel.UNLIMITED -- it creates LinkedListChannel
.
This is a channel with linked-list buffer of a unlimited capacity (limited only by available memory).
Sender to this channel never suspends and SendChannel.offer
always returns true
.
When capacity
is Channel.CONFLATED -- it creates ConflatedChannel
.
This channel buffers at most one element and conflates all subsequent send
and offer
invocations,
so that the receiver always gets the most recently sent element.
Back-to-send 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 SendChannel.offer
always returns true
.
When capacity
is positive, but less than UNLIMITED -- it creates array-based channel with given capacity.
This channel has an array buffer of a fixed capacity
.
Sender suspends only when buffer is fully and receiver suspends only when buffer is empty.
Modifier and Type | Interface and Description |
---|---|
static class |
Channel.Factory
Constants for channel factory function
Channel() . |
ReceiveChannel.DefaultImpls
Modifier and Type | Field and Description |
---|---|
static int |
CONFLATED
Requests conflated channel in
Channel(...) factory function -- the ConflatedChannel gets created. |
static Channel.Factory |
Factory
Constants for channel factory function
Channel() . |
static int |
RENDEZVOUS
Requests rendezvous channel in
Channel(...) factory function -- the RendezvousChannel gets created. |
static int |
UNLIMITED
Requests channel with unlimited capacity buffer in
Channel(...) factory function |
close, getOnSend, invokeOnClose, isClosedForSend, isFull, offer, send
cancel, getOnReceive, getOnReceiveOrNull, isClosedForReceive, isEmpty, iterator, poll, receive, receiveOrNull
static Channel.Factory Factory
Constants for channel factory function Channel()
.
static int UNLIMITED
Requests channel with unlimited capacity buffer in Channel(...)
factory function
static int RENDEZVOUS
Requests rendezvous channel in Channel(...)
factory function -- the RendezvousChannel
gets created.
static int CONFLATED
Requests conflated channel in Channel(...)
factory function -- the ConflatedChannel
gets created.