Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
32 lines (26 sloc) 918 Bytes
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
// This file was automatically generated from by Knit tool. Do not edit.
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
data class Ball(var hits: Int)
fun main() = runBlocking {
val table = Channel<Ball>() // a shared table
launch { player("ping", table) }
launch { player("pong", table) }
table.send(Ball(0)) // serve the ball
delay(1000) // delay 1 second
coroutineContext.cancelChildren() // game over, cancel them
suspend fun player(name: String, table: Channel<Ball>) {
for (ball in table) { // receive the ball in a loop
println("$name $ball")
delay(300) // wait a bit
table.send(ball) // send the ball back
You can’t perform that action at this time.