June 2018
Intermediate to advanced
316 pages
6h 34m
English
The produce function is an implementation of the producer-consumer pattern. The responsibility of a producer is to send messages to a queue. A consumer, in turn, consumes these messages:

Our new coroutinesProducerOrder function looks like this:
suspend fun coroutinesProducerOrder(amountOfCakes: Int) = produce<Cake> { (0 until amountOfCakes) .map { async { Baker().bake() } } .forEach { send(it.await()) }}.toList()
We can run this function using the following code:
fun main(args: Array<String>) = runBlocking { val cakes = Bakery().coroutinesProducerOrder(10) println("Number of cakes: ${cakes.size}")}
Read now
Unlock full access