Last active
January 12, 2020 20:33
-
-
Save juliuscanute/e0bd2155fe99d58ff8c2428da6ed12a9 to your computer and use it in GitHub Desktop.
[Offer vs Send] #kotlin #coroutine #offer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val fruitArray = arrayOf("Apple", "Banana", "Pear", "Grapes", "Strawberry") | |
val kotlinChannel = Channel<String>() | |
runBlocking { | |
launch { | |
for (fruit in fruitArray) { | |
val wasSent = kotlinChannel.offer(fruit) | |
if (wasSent) { | |
println("Sent: $fruit") | |
} else { | |
println("$fruit wasn't sent") | |
} | |
} | |
kotlinChannel.close() | |
} | |
for (fruit in kotlinChannel) { | |
println("Received: $fruit") | |
} | |
println("Done!") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sent: Apple | |
Banana wasn't sent | |
Pear wasn't sent | |
Grapes wasn't sent | |
Strawberry wasn't sent | |
Received: Apple | |
Done! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment