1 // This file was automatically generated from channels.md by Knit tool. Do not edit.
2 package kotlinx.coroutines.guide.exampleChannel09
3 
4 import kotlinx.coroutines.*
5 import kotlinx.coroutines.channels.*
6 
7 data class Ball(var hits: Int)
8 
<lambda>null9 fun main() = runBlocking {
10     val table = Channel<Ball>() // a shared table
11     launch { player("ping", table) }
12     launch { player("pong", table) }
13     table.send(Ball(0)) // serve the ball
14     delay(1000) // delay 1 second
15     coroutineContext.cancelChildren() // game over, cancel them
16 }
17 
playernull18 suspend fun player(name: String, table: Channel<Ball>) {
19     for (ball in table) { // receive the ball in a loop
20         ball.hits++
21         println("$name $ball")
22         delay(300) // wait a bit
23         table.send(ball) // send the ball back
24     }
25 }
26