Created
June 5, 2018 17:01
-
-
Save sanity/75d7b28b1e7690879020285631435d99 to your computer and use it in GitHub Desktop.
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
| import kotlinx.coroutines.experimental.* | |
| import kotlinx.coroutines.experimental.channels.* | |
| fun main(args: Array<String>) = runBlocking { | |
| val producer = produce { | |
| for (v in 0..100) { | |
| send(v) | |
| delay(100) | |
| } | |
| close() | |
| } | |
| fun doubler(producer : ReceiveChannel<Int>, consumer : SendChannel<String>) = launch { | |
| producer.consumeEach { v -> | |
| consumer.send((v * 2).toString()) | |
| delay(500) | |
| } | |
| } | |
| fun consumer(producer : ReceiveChannel<String>) = launch { | |
| producer.consumeEach { v -> | |
| println(v) | |
| } | |
| } | |
| val chan = Channel<String>() | |
| doubler(producer, chan) | |
| consumer(chan).joinChildren() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment