Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data class Ball(var hits: Int)
- fun main(args: Array<String>) = runBlocking<Unit> {
- val table = Channel<Ball>()
- launch { player("ping", table) }
- launch { player("pong", table) }
- table.send(Ball(0))
- delay(1000)
- coroutineContext.cancelChildren()
- }
- suspend fun player(name: String, table: Channel<Ball>) {
- for (ball in table) {
- ball.hits++
- println("$name $ball")
- // Comment out below delay to see the fairness a bit more.
- delay(300)
- table.send(ball)
- }
- }
Add Comment
Please, Sign In to add comment