Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example
- import kotlinx.coroutines.experimental.channels.Channel
- import kotlinx.coroutines.experimental.channels.actor
- import kotlinx.coroutines.experimental.channels.produce
- import kotlinx.coroutines.experimental.channels.toChannel
- import kotlinx.coroutines.experimental.runBlocking
- fun main(args: Array<String>) {
- val producer = produce<Int>(capacity = 5000) {
- var count = 0
- for(i in 1..100) {
- send(i)
- count++
- }
- println("Sent: $count numbers")
- }
- val consumer = actor<Int>(capacity = Channel.UNLIMITED) {
- var sum = 0
- var count = 0
- for(i in channel) {
- sum += i
- count++
- }
- println("Received: $count numbers")
- println("Sum: $sum")
- }
- runBlocking {
- producer.toChannel(consumer)
- }
- }
- Sent: 100 numbers
- Received: 100 numbers
- Sum: 5050
- Sent: 100 numbers
Add Comment
Please, Sign In to add comment