Guest User

Untitled

a guest
Feb 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. package com.example
  2.  
  3. import kotlinx.coroutines.experimental.channels.Channel
  4. import kotlinx.coroutines.experimental.channels.actor
  5. import kotlinx.coroutines.experimental.channels.produce
  6. import kotlinx.coroutines.experimental.channels.toChannel
  7. import kotlinx.coroutines.experimental.runBlocking
  8.  
  9. fun main(args: Array<String>) {
  10. val producer = produce<Int>(capacity = 5000) {
  11. var count = 0
  12. for(i in 1..100) {
  13. send(i)
  14. count++
  15. }
  16. println("Sent: $count numbers")
  17. }
  18.  
  19. val consumer = actor<Int>(capacity = Channel.UNLIMITED) {
  20. var sum = 0
  21. var count = 0
  22. for(i in channel) {
  23. sum += i
  24. count++
  25. }
  26. println("Received: $count numbers")
  27. println("Sum: $sum")
  28. }
  29.  
  30. runBlocking {
  31. producer.toChannel(consumer)
  32. }
  33. }
  34.  
  35. Sent: 100 numbers
  36. Received: 100 numbers
  37. Sum: 5050
  38.  
  39. Sent: 100 numbers
Add Comment
Please, Sign In to add comment