Skip to content

Instantly share code, notes, and snippets.

@juliuscanute
Last active January 12, 2020 20:33
Show Gist options
  • Save juliuscanute/e0bd2155fe99d58ff8c2428da6ed12a9 to your computer and use it in GitHub Desktop.
Save juliuscanute/e0bd2155fe99d58ff8c2428da6ed12a9 to your computer and use it in GitHub Desktop.
[Offer vs Send] #kotlin #coroutine #offer
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!")
}
}
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