Up until now, we've only discussed the usages of select as a receiver. But we can also use select to send items to another channel.
Let's look at the following example:
val batman = actor<String> {
for (c in this) {
println("Batman is beating some sense into $c")
delay(100)
}
}
val robin = actor<String> {
for (c in this) {
println("Robin is beating some sense into $c")
delay(250)
}
}
We have a superhero and their sidekick as two actors. Since the superhero is more experienced, it usually takes them less time to beat the villain they're facing.
But in some cases, they still have their hands full, so a sidekick needs to step in.
We'll throw five villains at the pair with a few delays, and see how they fare:
val j = launch {
for (c in listOf("Jocker", "Bane...