Created
November 7, 2018 18:47
-
-
Save mayojava/b5bbd9fa5030aaca744473e65d1d1b06 to your computer and use it in GitHub Desktop.
Consuming a channel once
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main() = runBlocking { | |
| val channel = Channel<Int>() | |
| launch { | |
| repeat(5) { | |
| channel.send(it*it) | |
| } | |
| channel.close() | |
| } | |
| println("begin") | |
| channel.consumeEach { | |
| println("consumed: $it") | |
| } | |
| println("again") | |
| channel.consumeEach { | |
| println("again consumed: $it") | |
| } | |
| } | |
| //console output | |
| begin | |
| consumed: 0 | |
| consumed: 1 | |
| consumed: 4 | |
| consumed: 9 | |
| consumed: 16 | |
| again |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment