Guest User

Untitled

a guest
Jan 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. data class Ball(var hits: Int)
  2.  
  3. fun main(args: Array<String>) = runBlocking<Unit> {
  4. val table = Channel<Ball>()
  5.  
  6. launch { player("ping", table) }
  7. launch { player("pong", table) }
  8.  
  9. table.send(Ball(0))
  10. delay(1000)
  11. coroutineContext.cancelChildren()
  12. }
  13.  
  14. suspend fun player(name: String, table: Channel<Ball>) {
  15. for (ball in table) {
  16. ball.hits++
  17. println("$name $ball")
  18. // Comment out below delay to see the fairness a bit more.
  19. delay(300)
  20. table.send(ball)
  21. }
  22. }
Add Comment
Please, Sign In to add comment