1 /* 2 * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 // This file was automatically generated from channels.md by Knit tool. Do not edit. 6 package kotlinx.coroutines.guide.exampleChannel09 7 8 import kotlinx.coroutines.* 9 import kotlinx.coroutines.channels.* 10 11 data class Ball(var hits: Int) 12 <lambda>null13fun main() = runBlocking { 14 val table = Channel<Ball>() // a shared table 15 launch { player("ping", table) } 16 launch { player("pong", table) } 17 table.send(Ball(0)) // serve the ball 18 delay(1000) // delay 1 second 19 coroutineContext.cancelChildren() // game over, cancel them 20 } 21 playernull22suspend fun player(name: String, table: Channel<Ball>) { 23 for (ball in table) { // receive the ball in a loop 24 ball.hits++ 25 println("$name $ball") 26 delay(300) // wait a bit 27 table.send(ball) // send the ball back 28 } 29 } 30