blob: ae2919fd07a586a232c339566453ec11a30b9836 [file] [log] [blame]
Roman Elizarovf16fd272017-02-07 11:26:00 +03001/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03002 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarovf16fd272017-02-07 11:26:00 +03003 */
4
Roman Elizarovb7721cf2017-02-03 19:23:08 +03005// This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
Roman Elizarova9687a32018-06-29 17:28:38 +03006package kotlinx.coroutines.experimental.guide.channel07
Roman Elizarovb7721cf2017-02-03 19:23:08 +03007
8import kotlinx.coroutines.experimental.*
9import kotlinx.coroutines.experimental.channels.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +030010import kotlin.coroutines.experimental.*
Roman Elizarovb7721cf2017-02-03 19:23:08 +030011
12suspend fun sendString(channel: SendChannel<String>, s: String, time: Long) {
13 while (true) {
14 delay(time)
15 channel.send(s)
16 }
17}
18
19fun main(args: Array<String>) = runBlocking<Unit> {
20 val channel = Channel<String>()
Roman Elizarov43e3af72017-07-21 16:01:31 +030021 launch(coroutineContext) { sendString(channel, "foo", 200L) }
22 launch(coroutineContext) { sendString(channel, "BAR!", 500L) }
Roman Elizarovb7721cf2017-02-03 19:23:08 +030023 repeat(6) { // receive first six
24 println(channel.receive())
25 }
Roman Elizarov8b38fa22017-09-27 17:44:31 +030026 coroutineContext.cancelChildren() // cancel all children to let main finish
Roman Elizarovb7721cf2017-02-03 19:23:08 +030027}