Kotlin In-Depth, [Vol. II]: A Comprehensive Guide to Modern Multi-Paradigm Language by Aleksei Sedunov

Kotlin In-Depth, [Vol. II]: A Comprehensive Guide to Modern Multi-Paradigm Language by Aleksei Sedunov

Author:Aleksei Sedunov [Sedunov, Aleksei]
Language: eng
Format: epub
Publisher: BPB Publications
Published: 2020-08-15T00:00:00+00:00


Channels

Channels offer you a convenient way to share an arbitrary data stream between coroutines. The basic operations on any channel represented by the Channel interface is sending data elements by the send() method and receiving them by the receive() method, respectively. When these methods can’t complete their work; for example, when the channel’s internal buffer is full and you try to send data to it, they suspend the current coroutine and resume them later when it’s possible. That’s the major difference between channels and blocking queues which play a similar role in Java’s concurrency API but work by blocking the calling thread.

Channels can be constructed by the generic Channel() function which takes an integer value describing the channel capacity. One of the basic implementations is a channel with internal buffer of a limited size. When the buffer is full, a call to send() is suspended until at least one element is received. Similarly, a call to receiver() is suspended when the buffer is empty until at least one element gets sent. Let’s consider an example:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.